[Insight-developers] Re: LightObject race condition fix

Steve M. Robbins steve at sumost.ca
Mon Aug 13 10:22:03 EDT 2007


On Mon, Aug 13, 2007 at 04:26:15PM +1000, Dan Mueller wrote:
> On 13/08/07, Steve M. Robbins <steve at sumost.ca> wrote:
> > On Wed, Aug 08, 2007 at 06:09:43AM +1000, Dan Mueller wrote:
> > > On 07/08/07, Steve M. Robbins <steve at sumost.ca> wrote:
> > > > Developers: what is the use-case for SetReferenceCount()?
> > >
> > > I know I use SetReferenceCount(): I have cases (to do with wrapping
> > > for .NET) where I do not know how many calls to Register() have gone
> > > before, but I do know when the object can be released. At that point I
> > > call SetReferenceCount(0) to force the pointer to be freed.
> >
> > OK, I'll bite: how do you know that the object can be released?
> >
> > I don't know anything about .NET and wrapping itk for it.  However, I
> > did once look into the java wrapping of vtk.  In that case, there is a
> > java proxy object for each C++ object and the proxy holds exactly one
> > reference to the C++ instance.  When the proxy is finalized, the
> > reference is decremented.  Can't this approach be used in ITK/.NET?
> 
> I use a similar approach to the vtk/java approach you describe: a
> managed wrapper object (read 'proxy object') keeps a reference to the
> underlying C++ object, and when the wrapper object is finalized the
> native reference can be freed.
> 
> As I mentioned previously, due to a C++/CLI limitation, the wrapper
> object can only keep a native pointer to any ITK object, not a
> SmartPointer. Therefore, I have found on subsequent calls to the
> wrapper methods the SmartPointer thinks it is going out of scope and
> frees itself. 

OK, now I'm even more confused.  First you say that the wrapper object
can only keep a native pointer.  Then you talk about "the SmartPointer".
What SmartPointer?


> To combat this, I call Register() on the native ITK
> pointer within every wrapper method to ensure the wrapped native
> object is kept alive. 

I had envisaged that the wrapper object has a native pointer as a
member variable, and therefore accessible from each of its methods.
If you add a reference using this pointer at wrapper construction time
and remove the reference at wrapper destruction, you should be done:
no need for calling Register() at each wrapper method.  

And no need for SetReferenceCount().

What am I missing?

-Steve
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.itk.org/mailman/private/insight-developers/attachments/20070813/550d1007/attachment.pgp


More information about the Insight-developers mailing list