Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api.': 0.04; 'explicitly': 0.04; 'compiler': 0.05; 'debug': 0.05; 'builtin': 0.07; 'statically': 0.07; 'api': 0.09; 'python': 0.09; 'corresponds': 0.09; 'malloc': 0.09; 'subject:ctypes': 0.09; 'switches': 0.09; 'allocates': 0.16; 'dll,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'msvcrt': 0.16; 'oct': 0.16; 'subject:which': 0.16; 'wrote:': 0.17; 'memory': 0.18; 'load': 0.19; 'changes': 0.20; 'received:209.85.214.174': 0.21; 'header :In-Reply-To:1': 0.25; 'am,': 0.27; 'dll': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'behaviour': 0.29; 'crash': 0.29; 'manual': 0.29; 'that.': 0.30; 'mode': 0.30; 'function': 0.30; 'figure': 0.30; 'code': 0.31; 'gets': 0.32; 'getting': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'version': 0.34; 'agree': 0.34; 'received:google.com': 0.34; 'done': 0.34; 'third': 0.34; 'similar': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'possible': 0.37; 'author': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'release': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'below,': 0.60; 'free': 0.61; 'provide': 0.62; 'safe': 0.63; 'different': 0.63; 'dangerous': 0.66; 'finally': 0.66; 'stated': 0.69; 'lean': 0.84; 'subject:free': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=xzpvP+M6cQKSuSf8Yd0X0qjY+9QVAkhbKErWhOD5puQ=; b=MuKxx/HzZQ0SPCf714hZXTDvp2RrPNTl+w6W9HOdoUCs72k655tfka8oLsxRE6vUg0 bMcWUrJiVVJlGLvFjULlsBZrI+i92CxrDI7vxxsZWWkyXvjA+uSti83aLfZ8xwkrGTt8 ho547x4dhMr1yUdvEuPBi/GXX6KdpZa5DOlXEUvWtU4z4iOH79ofcIb5S8gXgmqyHzfV TQbooqNygh7mTLbVql4ZK5RRAi2vAO7twCBC9uVW5Ywew+/+VzkcbBwJs4rwgaOzLDX6 ITNGcjWSwc2N3TnHQH75sFyG8K7GhgC4eUvLSMD/I/CeDaEp1MI3WFRJho0nBLoDmdw1 E0kQ== MIME-Version: 1.0 In-Reply-To: <65b67e08-e06e-4a6a-8bd5-eb6f0f74630f@googlegroups.com> References: <65b67e08-e06e-4a6a-8bd5-eb6f0f74630f@googlegroups.com> Date: Sun, 28 Oct 2012 07:26:42 +1100 Subject: Re: ctypes free memory which is allocated in C DLL From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351369606 news.xs4all.nl 6931 [2001:888:2000:d::a6]:41173 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32290 On Sun, Oct 28, 2012 at 2:40 AM, Ken Chen wrote: > Yes, I agree writing a corresponding API to free the memory is the best practice and best bet. > Sometimes, the third party API may not provide that. Then that's a majorly dangerous third party API. The only time it's safe to provide a half-only API like that is when the code gets statically linked with the application, so there's a guarantee that malloc() in one place corresponds to free() in another. > After digging the Python manual again and again. > I finally figure out why windll.msvcrt.free is failing. > > As the manual stated below, the DLL is using another version of msvcrt lib which is different than the builtin windll.msvcrt. After I explicitly load the msvcrt which built the DLL, things are getting function now. That's still vulnerable. There's really no guarantee about _anything_ with mismatched memory allocators; it's theoretically possible for compiler switches to change the behaviour of malloc/free such that compiling malloc in debug mode and free in release mode would crash your program. (I don't know off-hand of any compilers/libraries that do that specifically, but there are similar changes done in other ways.) If a DLL allocates memory and doesn't deallocate it, lean on its author to complete the job. ChrisA