« Return to Thread: newobject and feature("ref")

Re: newobject and feature("ref")

by Ben Webb :: Rate this Message:

Reply to Author | View in Thread

tahoma@... wrote:

> I want to use my own reference counting when wrapping my classes. When using
> %feature("ref") and %newobject the reference counter will be increased only in
> methods of classes derived from RefCountObject. I thought the ref/unref
> feature is applied to any return type derived from RefCountObject when
> %newobject is used.
>
> A small example is given in test.h/test.i. Creator1, Creator2 and a global
> function create instances of type RefCountObject, but only the reference
> counter of instances returned by Creator2 are increased. Creator2 is the only
> class that also derives from RefCountObject.
>
> Is that behaviour intended or did I miss something important? I want to use
> tool classes that do not derive from RefCountObject but creating or returning
> RefCountObject's.

I believe it's intended behavior - at least, that's what I see when I
use SWIG in a similar way. After all, SWIG does not know whether your
own creator methods are returning a 'new' reference or a 'borrowed'
reference (you could have written your creator methods so that they
increased the reference count, expecting that C++ callers of these
methods decrease the refcount when they're done). In cases where you
return a borrowed reference, you could always use a typemap to get your
desired behavior, such as:

%typemap(out) TYPE * {
   if (!($owner & SWIG_POINTER_NEW)) {
     // out typemaps are also called for constructors, which already
     // use %ref to increase the reference count. So don't do it twice.
     $1->incrementReferenceCount();
   }
   %set_output(SWIG_NewPointerObj(%as_voidptr($1), $descriptor(TYPE *),
$owner | SWIG_POINTER_OWN));
}

        Ben
--
ben@...                      http://salilab.org/~ben/
"It is a capital mistake to theorize before one has data."
        - Sir Arthur Conan Doyle

------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

 « Return to Thread: newobject and feature("ref")