<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 13/02/2014 14:22, Martin Dobias
wrote:<br>
</div>
<blockquote
cite="mid:CAC2XbFdVfiEEdtuOqAoJTnC=v3EUT83n_vuaF=XQKmr4ko979Q@mail.gmail.com"
type="cite">
<pre wrap="">On Thu, Feb 13, 2014 at 7:56 PM, Denis Rouzaud <a class="moz-txt-link-rfc2396E" href="mailto:denis.rouzaud@gmail.com"><denis.rouzaud@gmail.com></a> wrote:
</pre>
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">
2) If a SIP wrapper class A has a member class B and an instance of A
is created in Python, B member is referenced and reference to A is
deleted is it the A instance deleted by GC including B (which is still
referenced)?
C++: class A { B b; }
SIP: class A { B b; }
Python:
a = A()
b = a.b
a = None
# garbage collector
a and b were deleted?
</pre>
</blockquote>
<pre wrap="">
I would say yes too. But, I would ask confirmation to people more aware than
me. There should be some reading this ;)
</pre>
</blockquote>
<pre wrap="">
My understanding is following: with the line "a = None" the reference
count to SIP wrapper of A gets to zero. Because the object is owned by
Python (it was created in Python and the ownership was not transferred
to c++), also the underlying C++ object will be deleted. As far as I
know, "b" will still reference to SIP wrapper of B which in turn
references C++ object that has been deleted in the meanwhile. Using
"b" will may lead to crashes (or just strange behavior). If the class
"B" had a virtual destructor, the SIP wrapper for B (which is subclass
of B) should be at least be notified that the instance is going to be
deleted - so when trying to use "b" again, SIP can at least raise the
exception instead of crashing.
The above is my expectation of what happens, I have not actually tried that.
Martin
_______________________________________________
Qgis-developer mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/qgis-developer">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
</pre>
</blockquote>
I found that a bit surprising, so I did the test, an actually
python+sip do a pretty good job in ref counting (at least for the
mentioned classes which are pretty simple)<br>
<br>
I Added a dtor to QgsSnappingResult to see when it's called:<br>
<blockquote><tt>~QgsSnappingResult(){std::cout <<
__PRETTY_FUNCTION__ << "\n";}</tt><tt><br>
</tt></blockquote>
<br>
And the little script:<br>
<blockquote><tt>from qgis.core import *</tt><br>
<br>
<tt>print "try to del r while p is still referencing one of it's
members"</tt><br>
<tt>r = QgsSnappingResult()</tt><br>
<tt>p = r.snappedVertex</tt><br>
<tt>del r</tt><br>
<tt>print "it's not deleted yet"</tt><br>
<tt>print "delete the referencing guy"</tt><br>
<tt>del p</tt><br>
<tt>print "it's deleted"</tt><br>
<br>
<tt>print "try to del r (i.e. without anyone referencing
anything)"</tt><br>
<tt>r = QgsSnappingResult()</tt><br>
<tt>del r</tt><br>
<tt>print "it's deleted"</tt><br>
</blockquote>
<br>
Result:<br>
<blockquote><tt>try to del r while p is still referencing one of
it's members</tt><br>
<tt>it's not deleted yet</tt><br>
<tt>delete the referencing guy</tt><br>
<tt>QgsSnappingResult::~QgsSnappingResult()</tt><br>
<tt>it's deleted</tt><br>
<tt>try to del r (i.e. without anyone referencing anything)</tt><br>
<tt>QgsSnappingResult::~QgsSnappingResult()</tt><br>
<tt>it's deleted</tt><br>
</blockquote>
<br>
<br>
<br>
<br>
</body>
</html>