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



D'oh...

On 7 October 2017 at 18:07, Daniel Boles <dboles src gmail com> wrote:
After really thinking about this, I feel like I must've missed something obvious, and glibmm must keep a lazily initialised dictionary of wrapped widgets to free later, or something. :) But I suspect tracking down the relevant code is still beyond my current level of skill.

The bad news is that I didn't find this sooner. The good news is that I wasn't too dense to track it down! So, for posterity, here's what the derived wrap() functions ultimately call:


// This is a factory function that converts any type to
// its C++ wrapper instance by looking up a wrap_new() function in a map.
//
ObjectBase*
wrap_auto(GObject* object, bool take_copy)
{
  if (!object)
    return nullptr;

  // Look up current C++ wrapper instance:
  ObjectBase* pCppObject = ObjectBase::_get_current_wrapper(object);

  if (!pCppObject)
  {
    // There's not already a wrapper: generate a new C++ instance.
    pCppObject = wrap_create_new_wrapper(object);

    // [...]
  }

  // take_copy=true is used where the GTK+ function doesn't do
  // an extra ref for us, and always for plain struct members.
  if (take_copy)
    pCppObject->reference();

  return pCppObject;
}


So, glibmm owns the returned wrapper, and we users don't need to - and can't - mess with its lifetime. Nor do we need to worry about leaking things, so long as we leave take_copy = false. Then we just call wrap() whenever we want to create-or-get a wrapper for a C instance, and don't worry about it.


It would be nice to make this explicit in the docs; I'll write a small patch. The page I came from is this - https://developer.gnome.org/gtkmm-tutorial/stable/sec-basics-gobj-and-wrap.html.en - and could do with being slightly less terse.



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