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

  • Thread starter Christian Seberino
  • Start date
C

Christian Seberino

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
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

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
 
M

Mike Rovner

Christian said:
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
 
C

Christian Seberino

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
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top