473,326 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C extension question about avoiding memory leaks with the object returned...

I wrote some C functions and successfully imported them into Python.

All is apparently well but there is one question about the object returned
by the C function that is bugging me...

Take for example this last line of a C extension:

return Py_BuildValue("s", my_string);
If my_string allocated dynamically:
==========================
Will Python successfully handle garbage collection of my_string?
Will there be any problems because it was built within my C function?

(I would think Python will successfully receive a COPY of my_string
but that the original my_string object will be a memory leak!!!)

If my_string allocated statically:
==========================
Don't all statically declared objects get killed when you exit a C function?
This is not a problem because Python will get a COPY right?

(This seems "safer" until my dynamic question above gets answered but
with static guys you lose flexibility with the SIZE of the string of course.)

Any help would be greatly appreciated.

Chris
Jul 18 '05 #1
4 2337
se******@spawar.navy.mil (Christian Seberino) writes:
return Py_BuildValue("s", my_string);
In this case, Py_BuildValue is very expensive. Use PyString_FromString
instead.
If my_string allocated dynamically:
==========================
Will Python successfully handle garbage collection of my_string?
No. The resulting Python string object will be a copy; the original
string is considered read-only, and no attempt to release it is made.
Will there be any problems because it was built within my C function?
If you don't release it, there will be a memory leak. Apart from that:
no.
If my_string allocated statically:
==========================
Don't all statically declared objects get killed when you exit a C function?
You might need to explain what a statically declared string is, in
C. If you are talking about string literals: no, they don't get killed
when a C function exits. They exist from the start of the program
until it ends.
This is not a problem because Python will get a COPY right?


Right.

Regards,
Martin
Jul 18 '05 #2
Christian Seberino wrote:
return Py_BuildValue("s", my_string);
If my_string allocated dynamically:
==========================
Will Python successfully handle garbage collection of my_string?
Nope. It's your string, you handle it.
Will there be any problems because it was built within my C function?
No problems.
(I would think Python will successfully receive a COPY of my_string
but that the original my_string object will be a memory leak!!!)
Agreed.

So don't do that. Instead write (untested):

PyObject* ret = Py_BuildValue("s", my_string);
free(my_string);
return ret;
If my_string allocated statically:
==========================
Don't all statically declared objects get killed when you exit a C
function? This is not a problem because Python will get a COPY right?
Rigth.
AFAIUC, static objects "get killed" along with your whole program.
Probably you are talking about function local objects (dynamically)
allocated in stack.
(This seems "safer" until my dynamic question above gets answered but
with static guys you lose flexibility with the SIZE of the string of
course.)


"All what you're doing is safe as long as you understand what are you doing"
(c) not me

HTH,
Mike


Jul 18 '05 #3
Martin

Thanks. Is there no way to return a dynamically allocated C string in your
C extension without making a memory leak? Must all strings be allocated to be
a fixed size at compile time like this...???

#define MAX_LENGTH 20
....
char my_string[MAX_LENGTH];
....
return Py_BuildValue("s", my_string);

(This is what I meant by "statically allocated string".)

Will *this* make a memory leak as well?

Chris

ma****@v.loewis.de (Martin v. Löwis) wrote in message news:<m3************@mira.informatik.hu-berlin.de>...
se******@spawar.navy.mil (Christian Seberino) writes:
return Py_BuildValue("s", my_string);


In this case, Py_BuildValue is very expensive. Use PyString_FromString
instead.
If my_string allocated dynamically:
==========================
Will Python successfully handle garbage collection of my_string?


No. The resulting Python string object will be a copy; the original
string is considered read-only, and no attempt to release it is made.
Will there be any problems because it was built within my C function?


If you don't release it, there will be a memory leak. Apart from that:
no.
If my_string allocated statically:
==========================
Don't all statically declared objects get killed when you exit a C function?


You might need to explain what a statically declared string is, in
C. If you are talking about string literals: no, they don't get killed
when a C function exits. They exist from the start of the program
until it ends.
This is not a problem because Python will get a COPY right?


Right.

Regards,
Martin

Jul 18 '05 #4
se******@spawar.navy.mil (Christian Seberino) writes:
Thanks. Is there no way to return a dynamically allocated C string
in your C extension without making a memory leak? Must all strings
be allocated to be a fixed size at compile time like this...???
No. You will need to release the dynamically allocated string, after
calling PyString_FromString. How precisely to do that depends on how
precisely it was allocated, but assuming it was allocated with
malloc(3), you should release it with free(3).
#define MAX_LENGTH 20
...
char my_string[MAX_LENGTH];
...
return Py_BuildValue("s", my_string);

(This is what I meant by "statically allocated string".)

Will *this* make a memory leak as well?


No.

Regards,
Martin
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that...
4
by: Daniel | last post by:
I need to build the maze board(like 2d array) using a tree. But I find that my buildTree function doesn't work. Could anyone give me some hints on debugging it? Thanks bool BuildTree(TreeNodePtr...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
0
by: Frank Lopez | last post by:
Does anyone know if Microsoft generated a whitepaper on this topic? Does anyone know what the solution is? (meaning, eliminate the leak problem -- I am seeing three memory leaks from...
15
by: Chetan Raj | last post by:
Hi All, We have a web-application in asp.net that interacts with legacy code written in COM. The memory usage in aspnet_wp.exe increases every sec and never reduces. Using the .NET performance...
15
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different...
11
by: seberino | last post by:
Suppose a C extension locally built an array of PyObject* 's as follows... my_array = malloc(n * sizeof(PyObject*)); for (i = 0; i < n; i++) { my_array = PyList_New(0); } Q1: Must I do a...
2
by: =?Utf-8?B?Tm9tYW4gQWxp?= | last post by:
Hi, We are facing a strange problem in our ASP. NET website. Some times it gives the following unhandled exception. Error Message: Exception of type System.Web.HttpUnhandledException was...
2
by: Oltmans | last post by:
Hi all, I'm stuck in a situation where I need help. Any help will be highly appreciated. I've created an object, after creating the object, I'm assigning one of its functions to an event handler...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.