Hello. I found several codes using PyMem_Free to free allocated memory with PyMem_MALLOC (ie: PyUnicode_AsWideCharString) Is it safe?
Hirokazu Yamamoto wrote:
Hello. I found several codes using PyMem_Free to free allocated memory with PyMem_MALLOC (ie: PyUnicode_AsWideCharString)
Is it safe?
Within the interpreter: yes. In extensions: depends on the platform, but probably not. The macros provide faster access to the C lib malloc calls. The functions need to be used in extensions in case the interpreter will free the resource or the extension wants to free an interpreter allocated resource. They provide access to the malloc calls used by the interpreter, which may operate on a different heap than the extensions. Within an extension the macros use the extension heap. A subtle, but important difference. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Oct 30 2010)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
M.-A. Lemburg wrote:
Hirokazu Yamamoto wrote:
Hello. I found several codes using PyMem_Free to free allocated memory with PyMem_MALLOC (ie: PyUnicode_AsWideCharString)
Is it safe?
Within the interpreter: yes.
In extensions: depends on the platform, but probably not.
The macros provide faster access to the C lib malloc calls.
The functions need to be used in extensions in case the interpreter will free the resource or the extension wants to free an interpreter allocated resource. They provide access to the malloc calls used by the interpreter, which may operate on a different heap than the extensions.
Within an extension the macros use the extension heap.
A subtle, but important difference.
BTW: If you were referring to extensions using PyMem_Free() to deallocate memory allocated in the interpreter using PyMem_MALLOC(), then that's exactly how things should be done. I was referring to use of the two mentioned APIs within an extension. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Oct 30 2010)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
On 2010/10/31 2:32, M.-A. Lemburg wrote:
M.-A. Lemburg wrote:
Hirokazu Yamamoto wrote:
Hello. I found several codes using PyMem_Free to free allocated memory with PyMem_MALLOC (ie: PyUnicode_AsWideCharString)
Is it safe?
Within the interpreter: yes.
In extensions: depends on the platform, but probably not.
The macros provide faster access to the C lib malloc calls.
The functions need to be used in extensions in case the interpreter will free the resource or the extension wants to free an interpreter allocated resource. They provide access to the malloc calls used by the interpreter, which may operate on a different heap than the extensions.
Within an extension the macros use the extension heap.
A subtle, but important difference.
BTW: If you were referring to extensions using PyMem_Free() to deallocate memory allocated in the interpreter using PyMem_MALLOC(), then that's exactly how things should be done.
I was referring to use of the two mentioned APIs within an extension.
Thank you for reply, probably I could understand.
participants (2)
-
Hirokazu Yamamoto
-
M.-A. Lemburg