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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to