1 // Bug 564005 - Valgrind errors and crash on exit with Gtk::UIManager
2 // Bug 154498 - Unnecessary warning on console: signalproxy_connectionnode.cc
4 // libsigc++-only test case. (Or almost so. RefPtr is stolen from glibmm.)
6 // This test case is much more useful if it's run under valgrind.
8 #include "testutilities.h"
9 #include <sigc++/sigc++.h>
13 #define ACTIVATE_BUG 1
16 #ifndef _GLIBMM_REFPTR_H
17 #define _GLIBMM_REFPTR_H
19 /* Copyright 2002 The gtkmm Development Team
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2.1 of the License, or (at your option) any later version.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free
33 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 //#include <glibmmconfig.h>
41 /** RefPtr<> is a reference-counting shared smartpointer.
43 * Some objects in gtkmm are obtained from a shared
44 * store. Consequently you cannot instantiate them yourself. Instead they
45 * return a RefPtr which behaves much like an ordinary pointer in that members
46 * can be reached with the usual <code>object_ptr->member</code> notation.
47 * Unlike most other smart pointers, RefPtr doesn't support dereferencing
48 * through <code>*object_ptr</code>.
50 * Reference counting means that a shared reference count is incremented each
51 * time a RefPtr is copied, and decremented each time a RefPtr is destroyed,
52 * for instance when it leaves its scope. When the reference count reaches
53 * zero, the contained object is deleted, meaning you don't need to remember
54 * to delete the object.
56 * RefPtr<> can store any class that has reference() and unreference() methods.
57 * In gtkmm, that is anything derived from Glib::ObjectBase, such as
60 * See the "Memory Management" section in the "Programming with gtkmm"
61 * book for further information.
63 template <class T_CppObject>
67 /** Default constructor
69 * Afterwards it will be null and use of -> will cause a segmentation fault.
73 /// Destructor - decrements reference count.
76 /// For use only by the ::create() methods.
77 explicit inline RefPtr(T_CppObject* pCppObject);
81 * This increments the shared reference count.
83 inline RefPtr(const RefPtr<T_CppObject>& src);
85 /** Copy constructor (from different, but castable type).
87 * Increments the reference count.
89 template <class T_CastFrom>
90 inline RefPtr(const RefPtr<T_CastFrom>& src);
92 /** Swap the contents of two RefPtr<>.
93 * This method swaps the internal pointers to T_CppObject. This can be
94 * done safely without involving a reference/unreference cycle and is
95 * therefore highly efficient.
97 inline void swap(RefPtr<T_CppObject>& other);
99 /// Copy from another RefPtr:
100 inline RefPtr<T_CppObject>& operator=(const RefPtr<T_CppObject>& src);
102 /** Copy from different, but castable type).
104 * Increments the reference count.
106 template <class T_CastFrom>
107 inline RefPtr<T_CppObject>& operator=(const RefPtr<T_CastFrom>& src);
109 /// Tests whether the RefPtr<> point to the same underlying instance.
110 inline bool operator==(const RefPtr<T_CppObject>& src) const;
112 /// See operator==().
113 inline bool operator!=(const RefPtr<T_CppObject>& src) const;
117 * Use the methods of the underlying instance like so:
118 * <code>refptr->memberfun()</code>.
120 inline T_CppObject* operator->() const;
122 /** Test whether the RefPtr<> points to any underlying instance.
124 * Mimics usage of ordinary pointers:
130 inline explicit operator bool() const;
132 #ifndef GLIBMM_DISABLE_DEPRECATED
133 /// @deprecated Use reset() instead because this leads to confusion with clear() methods on the underlying class. For instance, people use .clear() when they mean ->clear().
135 #endif //GLIBMM_DISABLE_DEPRECATED
137 /** Set underlying instance to 0, decrementing reference count of existing instance appropriately.
142 /** Dynamic cast to derived class.
144 * The RefPtr can't be cast with the usual notation so instead you can use
146 * ptr_derived = RefPtr<Derived>::cast_dynamic(ptr_base);
149 template <class T_CastFrom>
150 static inline RefPtr<T_CppObject> cast_dynamic(const RefPtr<T_CastFrom>& src);
152 /** Static cast to derived class.
154 * Like the dynamic cast; the notation is
156 * ptr_derived = RefPtr<Derived>::cast_static(ptr_base);
159 template <class T_CastFrom>
160 static inline RefPtr<T_CppObject> cast_static(const RefPtr<T_CastFrom>& src);
162 /** Cast to non-const.
164 * The RefPtr can't be cast with the usual notation so instead you can use
166 * ptr_unconst = RefPtr<UnConstType>::cast_const(ptr_const);
169 template <class T_CastFrom>
170 static inline RefPtr<T_CppObject> cast_const(const RefPtr<T_CastFrom>& src);
172 /** Compare based on the underlying instance address.
174 * This is needed in code that requires an ordering on
175 * RefPtr<T_CppObject> instances, e.g. std::set<RefPtr<T_CppObject> >.
177 * Without these, comparing two RefPtr<T_CppObject> instances
178 * is still syntactically possible, but the result is semantically
179 * wrong, as p1 REL_OP p2 is interpreted as (bool)p1 REL_OP (bool)p2.
181 inline bool operator<(const RefPtr<T_CppObject>& src) const;
184 inline bool operator<=(const RefPtr<T_CppObject>& src) const;
187 inline bool operator>(const RefPtr<T_CppObject>& src) const;
190 inline bool operator>=(const RefPtr<T_CppObject>& src) const;
193 T_CppObject* pCppObject_;
197 #ifndef DOXYGEN_SHOULD_SKIP_THIS
199 // RefPtr<>::operator->() comes first here since it's used by other methods.
200 // If it would come after them it wouldn't be inlined.
202 template <class T_CppObject> inline
203 T_CppObject* RefPtr<T_CppObject>::operator->() const
208 template <class T_CppObject> inline
209 RefPtr<T_CppObject>::RefPtr()
211 pCppObject_ (nullptr)
214 template <class T_CppObject> inline
215 RefPtr<T_CppObject>::~RefPtr()
218 pCppObject_->unreference(); // This could cause pCppObject to be deleted.
221 template <class T_CppObject> inline
222 RefPtr<T_CppObject>::RefPtr(T_CppObject* pCppObject)
224 pCppObject_ (pCppObject)
227 template <class T_CppObject> inline
228 RefPtr<T_CppObject>::RefPtr(const RefPtr<T_CppObject>& src)
230 pCppObject_ (src.pCppObject_)
233 pCppObject_->reference();
236 // The templated ctor allows copy construction from any object that's
237 // castable. Thus, it does downcasts:
238 // base_ref = derived_ref
239 template <class T_CppObject>
240 template <class T_CastFrom>
242 RefPtr<T_CppObject>::RefPtr(const RefPtr<T_CastFrom>& src)
244 // A different RefPtr<> will not allow us access to pCppObject_. We need
245 // to add a get_underlying() for this, but that would encourage incorrect
246 // use, so we use the less well-known operator->() accessor:
247 pCppObject_ (src.operator->())
250 pCppObject_->reference();
253 template <class T_CppObject> inline
254 void RefPtr<T_CppObject>::swap(RefPtr<T_CppObject>& other)
256 const auto temp = pCppObject_;
257 pCppObject_ = other.pCppObject_;
258 other.pCppObject_ = temp;
261 template <class T_CppObject> inline
262 RefPtr<T_CppObject>& RefPtr<T_CppObject>::operator=(const RefPtr<T_CppObject>& src)
264 // In case you haven't seen the swap() technique to implement copy
265 // assignment before, here's what it does:
267 // 1) Create a temporary RefPtr<> instance via the copy ctor, thereby
268 // increasing the reference count of the source object.
270 // 2) Swap the internal object pointers of *this and the temporary
271 // RefPtr<>. After this step, *this already contains the new pointer,
272 // and the old pointer is now managed by temp.
274 // 3) The destructor of temp is executed, thereby unreferencing the
275 // old object pointer.
277 // This technique is described in Herb Sutter's "Exceptional C++", and
278 // has a number of advantages over conventional approaches:
280 // - Code reuse by calling the copy ctor.
281 // - Strong exception safety for free.
282 // - Self assignment is handled implicitely.
284 // - It just works and is hard to get wrong; i.e. you can use it without
285 // even thinking about it to implement copy assignment whereever the
286 // object data is managed indirectly via a pointer, which is very common.
288 RefPtr<T_CppObject> temp (src);
293 template <class T_CppObject>
294 template <class T_CastFrom>
296 RefPtr<T_CppObject>& RefPtr<T_CppObject>::operator=(const RefPtr<T_CastFrom>& src)
298 RefPtr<T_CppObject> temp (src);
303 template <class T_CppObject> inline
304 bool RefPtr<T_CppObject>::operator==(const RefPtr<T_CppObject>& src) const
306 return (pCppObject_ == src.pCppObject_);
309 template <class T_CppObject> inline
310 bool RefPtr<T_CppObject>::operator!=(const RefPtr<T_CppObject>& src) const
312 return (pCppObject_ != src.pCppObject_);
315 template <class T_CppObject> inline
316 RefPtr<T_CppObject>::operator bool() const
318 return (pCppObject_ != nullptr);
321 #ifndef GLIBMM_DISABLE_DEPRECATED
322 template <class T_CppObject> inline
323 void RefPtr<T_CppObject>::clear()
327 #endif //GLIBMM_DISABLE_DEPRECATED
329 template <class T_CppObject> inline
330 void RefPtr<T_CppObject>::reset()
332 RefPtr<T_CppObject> temp; // swap with an empty RefPtr<> to clear *this
336 template <class T_CppObject>
337 template <class T_CastFrom>
339 RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_dynamic(const RefPtr<T_CastFrom>& src)
341 const auto pCppObject = dynamic_cast<T_CppObject*>(src.operator->());
344 pCppObject->reference();
346 return RefPtr<T_CppObject>(pCppObject);
349 template <class T_CppObject>
350 template <class T_CastFrom>
352 RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_static(const RefPtr<T_CastFrom>& src)
354 const auto pCppObject = static_cast<T_CppObject*>(src.operator->());
357 pCppObject->reference();
359 return RefPtr<T_CppObject>(pCppObject);
362 template <class T_CppObject>
363 template <class T_CastFrom>
365 RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_const(const RefPtr<T_CastFrom>& src)
367 const auto pCppObject = const_cast<T_CppObject*>(src.operator->());
370 pCppObject->reference();
372 return RefPtr<T_CppObject>(pCppObject);
375 template <class T_CppObject> inline
376 bool RefPtr<T_CppObject>::operator<(const RefPtr<T_CppObject>& src) const
378 return (pCppObject_ < src.pCppObject_);
381 template <class T_CppObject> inline
382 bool RefPtr<T_CppObject>::operator<=(const RefPtr<T_CppObject>& src) const
384 return (pCppObject_ <= src.pCppObject_);
387 template <class T_CppObject> inline
388 bool RefPtr<T_CppObject>::operator>(const RefPtr<T_CppObject>& src) const
390 return (pCppObject_ > src.pCppObject_);
393 template <class T_CppObject> inline
394 bool RefPtr<T_CppObject>::operator>=(const RefPtr<T_CppObject>& src) const
396 return (pCppObject_ >= src.pCppObject_);
399 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
401 /** @relates Glib::RefPtr */
402 template <class T_CppObject> inline
403 void swap(RefPtr<T_CppObject>& lhs, RefPtr<T_CppObject>& rhs)
411 #endif /* _GLIBMM_REFPTR_H */
416 std::ostringstream result_stream;
418 class Action : public sigc::trackable
421 Action() : ref_count(1) { }
423 void reference() { ++ref_count; }
424 void unreference() { if (--ref_count <= 0) delete this; }
426 void emit_sig1(int n) { sig1.emit(n); }
428 sigc::signal<void, int>& signal_sig1() { return sig1; }
431 sigc::signal<void, int> sig1;
436 class Test : public sigc::trackable
442 result_stream << "new Test; ";
443 #ifdef ACTIVATE_BUG //See https://bugzilla.gnome.org/show_bug.cgi?id=564005#c14
444 action->signal_sig1().connect(sigc::bind(sigc::mem_fun(*this, &Test::on_sig1), action));
446 Glib::RefPtr<Action> action2(new Action);
447 action->signal_sig1().connect(sigc::bind(sigc::mem_fun(*this, &Test::on_sig1), action2));
453 result_stream << "delete Test; ";
456 void on_sig1(int n, Glib::RefPtr<Action> /* action */)
458 result_stream << "Test::on_sig1, n=" << n << "; ";
461 Glib::RefPtr<Action> action;
465 } // end anonymous namespace
467 int main(int argc, char* argv[])
469 auto util = TestUtilities::get_instance();
471 if (!util->check_command_args(argc, argv))
472 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
474 auto test = new Test;
476 test->action->emit_sig1(23);
478 util->check_result(result_stream, "new Test; Test::on_sig1, n=23; delete Test; ");
480 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;