On Thu, Sep 13, 2012 at 05:27:14PM -0400, Rajith Attapattu wrote: > >> Darryl, you could use %typemap(freearg) to ensure the respective free > >> function is called to cleanup. > >> How it gets called is also automatically handled by swig based on the > >> host language. > >> > > > > Maybe I'm missing something, but I don't think %typemap(freearg) helps in > > this case. From what I understand it's used to clean up temporary memory > > allocated for the purpose of argument processing. The situation I'm talking > > about is when you allocate an object in C and return it to the interpreter, > > e.g.: > > Sorry I misunderstood. I thought it was for methods. > > It appears Ruby does have support for %newobject > http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn59 > But it maybe simpler to use the strategy Rafi has used for the python binding.
The way I'm going at it currently is to have each constructor setup a
finalizer method that gets invoked when the object is garbage collected.
For example, in Messenger we have:
def initialize(name = nil)
@impl = Cproton.pn_messenger(name)
ObjectSpace.define_finalizer(self, self.class.finalize!(@impl))
end
def self.finalize!(impl) # :nodoc:
proc {
Cproton.pn_messenger_stop(impl)
Cproton.pn_messenger_free(impl)
}
end
What this does it register a class-level callback for each instance that
will call the finalize! method, passing in the underlying pn_messenger_t
instance. That will get stopped (if it's running) and then its resources
released.
--
Darryl L. Pierce, Sr. Software Engineer @ Red Hat, Inc.
Delivering value year after year.
Red Hat ranks #1 in value among software vendors.
http://www.redhat.com/promo/vendor/
pgpvA60OjevZy.pgp
Description: PGP signature
