Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > CPython and C++ object GC

Thread Tools

CPython and C++ object GC
Posts: n/a
Hello Everybody,

I would like to use a C++ gui library with the following (simplified)
interface in Python.

#include <stdio.h>

class Gui;

class GuiObject {
GuiObject(Gui *Gui) {printf("creating GuiObject(gui: %X)\n", Gui);}
~GuiObject() {printf("deleting GuiObject\n");}
void Move(int x, int y) {printf("GuiObject move(%d, %d)\n", x, y);};

class Gui {
Gui() {printf("creating Gui\n");}
~Gui() {printf("deleting Gui\n");}
GuiObject* AddImage() {
GuiObject* ob = new GuiObject(this);
return ob;
void Print() {printf("Gui: %X\n", this);}

int main() {
Gui *gui = new Gui();
GuiObject *obj = gui->AddImage();
obj->Move(50, 50);
/*GuiObject *obj2 = new GuiObject(gui); // not allowed
delete obj2;*/
delete obj;
delete gui;
return 0;

I created the Python Gui and GuiObject classes (PyTypeObject), and
added it to main module (PyModule_AddObject).
It works, but there is a problem at the Gui::AddImage(), with
constructs a new GuiObject, which is available in Python layer but
finally it is not collected and freed by GC:

obj = _PyObject_New(&GuiObjectType);
PyObject_Init(obj, &GuiObjectType);

I cannot invoke the GuiObject object constructor directly from Python,
because of the implementation of the C++ gui library (in this case it
would be collected).
I use the embedded CPython as an interpreter, so I cannot add
additional external .py file for it.

So the following Python code would be the target:

gui = GUI();

background = gui.AddImage();
#background = GuiObject(gui); <-- Collected but not allowed
background.Move(0, 0);

How could I implement the AddImage function in order to be freed the
constructed object at the end?

Thanks in advance!


Reply With Quote
Posts: n/a
[posted & e-mailed]

In article <(E-Mail Removed)>,
<(E-Mail Removed)> wrote:
>I would like to use a C++ gui library with the following (simplified)
>interface in Python.

Given the lack of responses on, try the capi-sig mailing list.
Aahz ((E-Mail Removed)) <*>

"...string iteration isn't about treating strings as sequences of strings,
it's about treating strings as sequences of characters. The fact that
characters are also strings is the reason we have problems, but characters
are strings for other good reasons." --Aahz
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
__class__ and type() implementation details in CPython Ivan Yurchenko Python 1 02-10-2013 01:59 PM
Can python (CPython) and IPython coexist normally on the same computer ? scoop Python 4 03-12-2009 01:51 AM
CPython and a C extension using Boehm GC malkarouri Python 5 12-26-2007 08:28 PM
Difference between CPython, Python for .NET and IronPython? Carl Johan Rehn Python 3 02-20-2006 10:47 PM
Same GUI in both CPython and Jython? York Python 1 09-10-2003 06:25 PM