CPython and C++ object GC

C

csaba.balazs

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 {
public:
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 {
public:
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();
gui->Print();
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.ImageFile("bg.jpg");
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!

All-the-best,

Csaba
 
A

Aahz

[posted & e-mailed]

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

Given the lack of responses on c.l.py, try the capi-sig mailing list.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"...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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top