From: John L. <jl...@ma...> - 2007-07-19 00:33:34
|
On 07/18/2007 03:53 PM, Abel Gezahegne wrote: > John Lenz wrote: > >> On 07/18/2007 01:24 PM, Abel Gezahegne wrote: >> >> >>> Hi >>> >>> I needed to allocate memory in a method I've added to a class using >>> %extend. This is for the __str__() method, the allocated memory is >>> returned by the function. I'm doing this instead of a fixed size char >>> array because the output string can vary a lot. The pointer to the new >>> array is a static variable. In order to remove the allocated memory I >>> added "%newobject __str__;" My question is, does this remove the memory >>> after the function call or when there are no more references to the >>> class in question? From my testing it seems to do the former, I just >>> wanted to confirm this. And if that is the case, can any one suggest a >>> different way I might go about this? (My target language is python) >>> Thank you for any thoughts. >>> >>> >>> >> The python typemaps are a little hard to read, but I think the default >> python string typemaps use PyString_FromStringAndSize, which I believe >> copies the data to an internal string. >> >> http://docs.python.org/api/stringObjects.html >> >> So your function is returning an allocated array, but then the data is >> just copied into a python string. I would think the newly allocated >> array should then be immediately freed. >> >> With strings, you also may need a newfree typemap >> http://www.swig.org/Doc1.3/Typemaps.html#Typemaps_nn34 >> http://www.swig.org/Doc1.3/Customization.html#ownership >> >> John >> >> >> > Hi John, > > From the document you have pointed out, in section Object ownership and > %newobject, the memory is deallocated after the call. However, in my > case, that is a problem. My intention is to minimize the number of > allocation I have to do, I wouldn't want to allocate memory each time > the method is called, and done so by different instances of the same > class. Thus, what I hoped to do was allocated once, (with resize if > needed) and delete the array completely not when the function is done, > but when there are no instances of that class. Is this possible? > As I mentioned above, python will internally make a copy the string. see http://docs.python.org/api/stringObjects.html Thus at the end of the function call, whatever buffer you create will no longer be used anywhere, and should be deleted. I am not sure what PyString_InternFromString, if that makes a copy or not. It might be what you are looking for. You really need to investigate how python will store the string. You will need to find some way to get python to create a python string, that points to your internal buffer. (I don't know if this is possible or not) You can then create a custom typemap, which calls whichever python functions are needed. Then apply that typemap to the __str__ function. Alternatively, you could return a wrapped pointer object instead of a python string from __str__, but I don't think that meets the conventions... the __str__ function is the conversion to a string so should probably return a python string... If you returned a wrapped pointer object, then we would register with the python garbage collector, and so delete whenever the last reference. The main issue you face is that you are trying to return a python string, not a wrapped pointer. John |