After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 547306 - Hard to get a RefPtr<> to this.
Hard to get a RefPtr<> to this.
Status: RESOLVED OBSOLETE
Product: glibmm
Classification: Bindings
Component: general
2.17.x
Other All
: Normal normal
: ---
Assigned To: gtkmm-forge
gtkmm-forge
Depends on: 755037
Blocks:
 
 
Reported: 2008-08-11 15:54 UTC by gonen
Modified: 2018-05-22 12:09 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
patch: RefPtr: Add constructor RefPtr(T_CppObject* pCppObject, bool take_ref). (2.34 KB, patch)
2013-05-21 18:04 UTC, Kjell Ahlstedt
none Details | Review
testcase.cc (1.73 KB, text/plain)
2013-05-21 18:05 UTC, Kjell Ahlstedt
  Details
0001-TextMark-Avoid-creating-a-RefPtr-to-this.patch (2.14 KB, patch)
2017-04-05 11:40 UTC, Murray Cumming
none Details | Review

Description gonen 2008-08-11 15:54:07 UTC
Hi,

I'm writing a button actor, that inherits from clutter::actor, and I want to use an effectTemplate when the button is pressed.

I wrote the following code in my function button_actor::real_released()

m_press_effect->fade(this, 0x44,sigc::bind(sigc::ptr_fun(do_nothing), m_press_effect));

m_press_effect is of type Clutter::EffectTemplate, and the problem is that fade's first parameter is not actor pointer, but a actor smart pointer Glib::RefPtr<Clutter::Actor>).

I cannot wrap this with smart pointer like :

m_press_effect->fade(Glib::RefPtr<Clutter::Actor>(this), 
0x44,sigc::bind(sigc::ptr_fun(do_nothing), m_press_effect));

because at the end of the function, the reference count will go to 0, and it will do 'delete this' which is very bad.

Is there any other way to write this? I wonder how GTKmm and other mm projects solve similar problems like this.

Another small problem, is that  some functions in clutter can get NULL, but in case you use reference, you cannot pass NULL.
Comment 1 Murray Cumming 2008-08-11 17:48:16 UTC
I agree that this is not obvious and it is ugly, but you can do this:

Glib::RefPtr<MyClass> refptr_this = Glib::RefPtr<MyClass>(this);
refptr->reference();

I wonder how we could make this easier.
Comment 2 gonen 2008-08-12 13:23:17 UTC
Wouldn't this solution create a memory leak?
Comment 3 Murray Cumming 2008-08-13 07:19:19 UTC
No.(In reply to comment #2)
> Wouldn't this solution create a memory leak?

No, that initial reference is necessary, because the constructor doesn't take a reference (because it's meant for use by create() functions that know whether they need to do this. Remember it's not a general purpose smartpointer.) The reference will be unreferenced by the destructor.


Comment 4 Daniel Elstner 2009-09-20 08:22:49 UTC
Not obvious and ugly?  Now that's an understatement.  I think the RefPtr<> ctor should acquire a reference by default, and I'm going to propose that change (and many others) for gtkmm 3.
Comment 5 Kjell Ahlstedt 2013-05-21 18:04:54 UTC
Created attachment 244979 [details] [review]
patch: RefPtr: Add constructor RefPtr(T_CppObject* pCppObject, bool take_ref).

wrap() functions have a take_copy parameter. We could add a RefPtr constructor
with a similar parameter, take_ref.
Comment 6 Kjell Ahlstedt 2013-05-21 18:05:48 UTC
Created attachment 244980 [details]
testcase.cc

Test case, testing the proposed constructor.
Comment 7 Murray Cumming 2013-07-31 08:07:01 UTC
C++11's std::shared_ptr<> deals with this problem by allowing the contained class to derive from std::enable_shared_by_this so you can then call shared_by_this().
http://en.cppreference.com/w/cpp/memory/enable_shared_from_this

Maybe we should consider something similar, for consistency.


Of course, it would also be nice if we could somehow replace Glib::RefPtr with std::sharedptr<>, but I don't see any way to specify any extra class to do the underlying cleverness.
Comment 8 Murray Cumming 2016-04-10 11:23:45 UTC
I'm making this bug dependent on bug #755037 , because this would be simplified by us just using std::shared_ptr instead of RefPtr.
Comment 9 Murray Cumming 2017-04-05 11:40:57 UTC
Created attachment 349287 [details] [review]
0001-TextMark-Avoid-creating-a-RefPtr-to-this.patch
Comment 10 Murray Cumming 2017-04-05 11:41:16 UTC
Sorry, I put that patch on the wrong bug.
Comment 11 Daniel Boles 2017-10-09 12:40:11 UTC
Isn't this obsolete now? glibmm master uses std::shared_ptr as Glib::RefPtr, so that side seems covered. And the stable branch presumably won't add such a change.
Comment 12 Kjell Ahlstedt 2017-10-10 15:10:18 UTC
The problem with Glib::RefPtr(this) is hardly solved by using std::shared_ptr.
See several comments in bug 775037, such as comments 26, 27, 28, 37, 39.
Murray had some trouble removing all internal RefPtr(this) in gtkmm.

Should this bug be closed as Wontfix?

Or add
template <class T_CppObject>
RefPtr<T_CppObject>
make_refptr_for_this(T_CppObject* object)
{
  object->reference();
  return RefPtr<T_CppObject>(object, &RefPtrDeleter<T_CppObject>);
}
analogous to Glib::make_refptr_for_instance()?
Comment 13 GNOME Infrastructure Team 2018-05-22 12:09:06 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glibmm/issues/8.