Returning a Pointer From a Function

View: New views
2 Messages — Rating Filter:   Alert me  

Returning a Pointer From a Function

by Gregory Bayard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello fellow swig enthusiasts,

If I extend a C++ class with a function that returns a pointer to a
newly instantiated object is there a way to force SWIG/Python to take
ownership of the object without me using ConvertPtr and returning a
PyObject* instead.  Here is my example:

%extend MyClass
{
   MyClassHelper* new_helper()
   {
      MyClassHelper *newHelper = new MyClassHelper();
      /* DO SOME IMPORTANT STUFF HERE*/
      return newHelper;
   }
}

It appears that these new helper objects never get deleted using this
code.  Any help is greatly appreciated!

Thanks,
Greg

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Returning a Pointer From a Function

by Josh Cherry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Tue, 3 Jun 2008, Gregory Bayard wrote:

> If I extend a C++ class with a function that returns a pointer to a
> newly instantiated object is there a way to force SWIG/Python to take
> ownership of the object without me using ConvertPtr and returning a
> PyObject* instead.

Have you tried %newobject (covered in the manual)?

Josh


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user