Memory Management and DO
Memory Management and DO
- Subject: Memory Management and DO
- From: Thierry Faucounau <email@hidden>
- Date: Sun, 17 Jun 2001 16:37:41 -0600
Hi,
I've been using ObjectAlloc to track memory leaks in my app and have
empirically determined the following behavior which I would like to
confirm is correct.
The app is split up into two threads and uses DO to communicate as shown
in the docs for NSConnection (I think).
When the worker thread (thread 2) needs an interface object (like a
window), it sends a DO message to the main thread. At this point, the
controller on the other side of the DO connection creates a custom
object which in turns creates any needed AppKit objects. This object is
returned to the worker thread by putting it in an NSDictionary and
sending that back as the return value (it is then reatined by the worker
thread upon receipt). When the worker thread is finished with it, it
sends another DO message with the object in an other NSDictionary back
to the AppKit thread which then tells the object to release the AppKit
objects and finally it releases the custom object. The worker thread
then releases it's proxy.
When I profiled this using ObjectAlloc, I noticed that all of the AppKit
things were freed but the custom object which got passed back and forth
was not. A quick peek at it's retainCount from gdb (using: print
(int)[object retainCount] from the gdb console) showed that I was one
off. The fix seems to be to autorelease the object before I send it back
to the worker thread after creation in the AppKit thread. This causes it
to be properly freed.
Hopefully that wasn't too confusing.
So I have two questions:
-Is what I'm doing correct?
(It does work, I can create/release as many objects as I want and they
all behave and free as expected. I'm actually afraid that I may be
compensating for a bug somewhere in the system which when fixed will
cause my app to crash when trying to use un-allocated objects.)
-Why do I need the seemingly extra autorelease? (Or phrased differently,
where is the extra retain being called?)
Thanks for any enlightenment.
--
Thierry Faucounau
Research Systems, Inc.