Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.wiretrip.org!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'pointer': 0.05; 'fine,': 0.07; 'implicit': 0.07; 'ret': 0.07; ':-)': 0.07; 'called.': 0.09; 'description.': 0.09; 'question:': 0.09; 'api': 0.11; 'above?': 0.16; 'allocate': 0.16; 'answer,': 0.16; 'borrowed': 0.16; 'btw:': 0.16; 'ever...': 0.16; 'great!': 0.16; 'pyobject*': 0.16; 'reference"': 0.16; 'ret;': 0.16; 'subject:() ': 0.16; 'code.': 0.18; 'input': 0.18; 'maybe': 0.21; '(which': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'doc': 0.23; 'vs.': 0.23; 'example': 0.24; 'memory': 0.24; "didn't": 0.25; 'parameters': 0.26; 'object': 0.27; "doesn't": 0.28; 'thanks': 0.29; 'depends': 0.29; "python's": 0.29; 'probably': 0.30; 'called': 0.32; 'to:addr :python-list': 0.32; 'another': 0.32; "i've": 0.33; 'sorry': 0.33; 'using': 0.34; 'there': 0.35; 'concepts': 0.35; 'header:User- Agent:1': 0.35; 'hello,': 0.36; 'some': 0.37; 'should': 0.37; 'be,': 0.38; 'but': 0.38; 'so,': 0.38; 'to:addr:python.org': 0.39; 'could': 0.39; 'where': 0.39; 'how': 0.39; 'works': 0.40; 'would': 0.40; 'header:Received:5': 0.40; 'received:86': 0.60; 'free': 0.62; 'and,': 0.63; 'faster...': 0.84; 'received:hu': 0.84 X-Spam-Score: -1.345 X-Spam-Level: X-Spam-Status: No, score=-1.345 tagged_above=-20 required=4 tests=[AWL=0.024, BAYES_00=-2.599, DNS_FROM_OPENWHOIS=1.13, RDNS_NONE=0.1] Date: Tue, 26 Apr 2011 16:03:21 +0200 From: =?utf-8?Q?Heged=C3=BCs?= Ervin To: python-list@python.org Subject: Re: Py_INCREF() incomprehension References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 59 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303826381 news.xs4all.nl 41114 [::ffff:82.94.164.166]:37287 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4043 Hello, thanks for the answer, > >Everything works fine, but sorry for the recurrent question: where > >should I use the Py_INCREF()/Py_DECREF() in code above? > > That depends on the functions which are called. It should be given > in the API description. The same counts for the incoming parameters > (which are borrowed AFAIR - but better have a look). I've read API doc (which you've included in another mail), but that's not clear for me. :( > The most critical parts are indeed > > * the input parameters > > and > > * Py_BuildValue() > > . Maybe you could as well have a look at some example code. > > Especially look at the concepts called "borrowed reference" vs. > "owned reference". > > And, for your other question: > > > if (cRes == 0) { > > return Py_BuildValue("s", outdata); > > } > > You ask how to stop leaking memory? Well, simply by not leaking it :-) great! :) > Just free the memory area: > > if (cRes == 0) { > PyObject* ret = Py_BuildValue("s", outdata); > free(outdata); > return ret; > } so, it means when I implicit allocate a new object (whit Py_BuildValue()), Python's GC will free that pointer when it doesn't require anymore? > BTW: Is there any reason for using calloc()? malloc() would probably > be faster... may be, I didn't measure it ever... but calloc() gives clear space... :) thanks: a.