Python C API (PyObject_CallMethod clear object)

  • Thread starter googler.1.webmaster
  • Start date
G

googler.1.webmaster

Hi!

I have a problm with deallocating stuff. I call a function with this
command:

PyObject *rvalue = PyObject_CallMethod(obj, "execute","",NULL);

if(rvalue==NULL)
PyErr_Print();
else
Py_DECREF(rvalue);

Can it be, that something is missing here? Imagine I allocate an
object of a type:

t = MyImage()
,;- syntax_error 129=)/% #is a syntax error

How you see I would get a syntaxerror, but an object of the type
MyImage() is created. But its deallocated when Py_Finalize() is
called. What can I do to deallocate this object after
PyObject_CallMethod returns NULL, too? I debug it, so if rvalue!=NULL
the object is deallocated, but what can I do if rvalue==NULL=


Thanks a lot!!!!
 
G

Gabriel Genellina

I have a problm with deallocating stuff. I call a function with this
command:

PyObject *rvalue = PyObject_CallMethod(obj, "execute","",NULL);

if(rvalue==NULL)
PyErr_Print();
else
Py_DECREF(rvalue);

Can it be, that something is missing here? Imagine I allocate an
object of a type:

t = MyImage()
,;- syntax_error 129=)/% #is a syntax error

How you see I would get a syntaxerror, but an object of the type
MyImage() is created. But its deallocated when Py_Finalize() is
called. What can I do to deallocate this object after
PyObject_CallMethod returns NULL, too? I debug it, so if rvalue!=NULL
the object is deallocated, but what can I do if rvalue==NULL=

A syntax error is detected *before* the code runs -- so the object isn't
created at all.
 
G

googler.1.webmaster

En Sun, 25 Jan 2009 23:46:01 -0200, <[email protected]>  
escribió:









A syntax error is detected *before* the code runs -- so the object isn't  
created at all.

Hi!

Yes, you are right. I got this issue not in syntax error, I get it on
a runtime error like this:

t = MyImage()
self.test("hallo") #test takes an integer so this would throw an
exception.


Here you see, an object of the type MyImage() was created but its not
deleted. 't' is a local reference so how can I do that?


Thanks!
 
G

googler.1.webmaster

the hook is, how to delete the locals of this function, maybe thats a
workaround?

thanks and bye.
 
G

Gabriel Genellina

Yes, you are right. I got this issue not in syntax error, I get it on
a runtime error like this:

t = MyImage()
self.test("hallo") #test takes an integer so this would throw an
exception.

Here you see, an object of the type MyImage() was created but its not
deleted. 't' is a local reference so how can I do that?

It will be deleted after the last reference to it is released, as every
object in Python. If the above two lines are the whole function code, this
will happen when exiting the function. If some other object holds a
reference to this MyImage instance (by example, by doing xxx.attribute =
t, or inserting it in a list) then that won't happen until all those
references are released.
Note that, in Python 2.x, the traceback object holds a reference to all
local variables -- if you store the traceback somewhere, those objects
won't be deleted.
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top