CPython and C++ object GC

Discussion in 'Python' started by csaba.balazs@gmail.com, Mar 30, 2009.

  1. Guest

    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
    , Mar 30, 2009
    #1
    1. Advertising

  2. Aahz Guest

    [posted & e-mailed]

    In article <>,
    <> wrote:
    >
    >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 () <*> 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
    Aahz, Apr 5, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. York
    Replies:
    1
    Views:
    367
    Chris Liechti
    Sep 10, 2003
  2. Carl Johan Rehn
    Replies:
    3
    Views:
    1,611
    Ian Bicking
    Feb 20, 2006
  3. malkarouri
    Replies:
    5
    Views:
    267
    MrJean1
    Dec 26, 2007
  4. scoop
    Replies:
    4
    Views:
    1,121
    John Machin
    Mar 12, 2009
  5. Ivan Yurchenko
    Replies:
    1
    Views:
    324
    Steven D'Aprano
    Feb 10, 2013
Loading...

Share This Page