How to use wrap() to get temporary C++ views of longer-lived C instances



If I have an existing C widget and just want to use C++ syntax with it for a while, what exactly do I do? Well, OK - obviously, I call Gtk::Whatever::Wrap(c_instance) and get a pointer to a C++ class from that.

But what are the ownership semantics of that pointer? I'm responsible for deleting it, right?

If so, then why do some parts of the gtkmm implementation call wrap(), do something with the pointer, then discard it, without ever deleting or unreffing? Is that leaking references to the C instance, or what did I miss?

e.g.:
 * https://git.gnome.org/browse/gtkmm/tree/gtk/src/menushell.ccg?h=gtkmm-3-22#n73
 * https://git.gnome.org/browse/gtkmm/tree/gtk/src/printjob.ccg?h=gtkmm-3-22#n20


Basically, my widget already exists in C and must continue to exist there. I just want to return a wrapped C++ instance from a helper method and use it in a few places, so I zget nicer/consistent syntax.

My gut feeling was to call wrap(c_instance, true) so that I just take a ref instead of ownership, then have my method return a unique_ptr<>, which would result in the C++ instance being deleted and (because I called wrap() with take_copy = true) unreffed from my side.

But this seems somehow to result in my C instance going away, lots of criticals, then a segfault, etc. So, presumably the unique_ptr isn't right.


So, *can* I just use gtkmm temporarily like this? How do I do it? Can we improve the documentation so that  lifetime and deletion of the returned wrapper are clear? And might we need to fix some parts of gtkmm itself?

Thanks!



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]