Re: Memory Management Question
Re: Memory Management Question
- Subject: Re: Memory Management Question
- From: "Clark Cox" <email@hidden>
- Date: Mon, 2 Jul 2007 09:06:07 -0700
On 7/2/07, Seth Pellegrino <email@hidden> wrote:
Hello all,
I'm getting a lot of EXC_BAD_ACCESS errors from my code, mostly
because I call [object release] and then later on call [object
someOtherMethod].
When you release something, you are, in effect, telling the runtime
that your are done with that object, and that you no longer care about
what happens to it. If somebody else still cares, then it is their
responsibility to have retained it. If you've released it, then why
are you still sending messages to it?
The workaround I've found is that every time I call
[object release] I then assign object = nil,
This can actually hide bugs in your design if not done carefully.
and every time I'm
working with a possibly dead object, I wrap the code in an if
(object) { } block.
Well, if the object is already nil, then sending a message to it is
essentially a no-op; so your check is probably redundant.
That seems like an odd way to manage objects, though. Am I doing
something wrong,
You're treating the symptoms instead of treating the underlying
problem. It would be better to actually track down *why* you are still
sending messages to a dead object, and exactly *which* object it is. I
would recommend becoming familiar with NSZombie:
<http://developer.apple.com/technotes/tn2004/tn2124.html>
<http://www.cocoadev.com/index.pl?DebuggingAutorelease>
Essentially, you run your application with the "NSZombieEnabled"
environment variable set to "YES". When this is done, instead of
actually deallocating objects that have been completely released, it
actually replaces them in place with a "zombie" object. Then, when a
message is later sent to that object, the zombie object will print a
message telling you which object was over-released.
This, used in conjunction with other debugging aids (such as malloc
stack logging) can usually halp you to track the problem down very
quickly.
or is there at least a way to compress the "release and assign nil"
block into one line?
No, unless you want to use a preprocessor macro (which I would advise against).
--
Clark S. Cox III
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden