On Monday, February 3, 2003, at 09:34 PM, Thilo Planz wrote:

I am getting pretty close to releasing some Camelbones code on Sourceforge, although I am not sure if it can serve as an example to others.
(It will feature a partly functional browser view, system services, add-on services, pasteboard access and a dynamic dock menu though).
Sounds sweet. Let me know when it's up - I'm putting together a links page for the CB site.

But first I have a question about memory management:
In the Objective-C samples there is always a great deal of retains and releases.
Do I need to care about that in Camelbones?
In theory you don't need to worry about it; whenever a Perl "wrapper" object is created, it issues a "retain" on the object it wraps. Similarly, when a Perl "wrapper" is GC'd, it should also release the underlying Objective-C object that it wraps.

In practice, this scheme isn't foolproof. For one thing, I chose to err on the side of caution - meaning that, since an extra release results in a core dump, while an extra retain merely leaks some memory, I chose to be quite liberal with retains. There are definitely some leaks in the CB framework that I need to chase down.

For another thing, it breaks when you create an object with "init*" or "copy". These methods return an object whose retain count is already set to one, so the retain issued when the Perl wrapper object is created is redundant. When the wrapper is GC'd and the ObjC object released, the retain count only drops back down to one, not zero - so the object leaks.

One way I've found to deal with this is to immediately "autorelease" any objects I create with alloc->init. This balances the initial retain, so when the Perl wrapper is GC'd, the release that's issued then should bring the retain count to zero and allow the ObjC object to be deallocated.

To be honest, I wouldn't worry too much about it for now. However, if your find that your app is leaking excessively, and ObjectAlloc tells you there are a great many NSMenu objects being allocated but not released, you can try creating them like this:

my $menu = NSMenu->alloc->init->autorelease;

sherm--

C programmers never die - they're just cast into void.



Reply via email to