Re: Memory Management question
Re: Memory Management question
- Subject: Re: Memory Management question
- From: "Michael Ash" <email@hidden>
- Date: Fri, 26 Dec 2008 12:28:04 -0500
On Fri, Dec 26, 2008 at 11:27 AM, Robert Marini <email@hidden> wrote:
> 1) desiredURL and link are, in essence, the same object. As far as the
> system is aware though, only one of those (link) is responsible for it.
The system is not aware of responsibility at all. By the time memory
management happens, all concept of local variables has disappeared.
The system cannot know, and does not care, what variables are
"responsible" for an object. All it cares about is properly balanced
retains and releases.
> It
> is entirely possible that link is being sent -release in another method
> executing on another thread and so you absolutely should retain the variable
> (*any* variable, really) used in your method when you enter into it and
> release it when you are ready to exit.
This is very wrongheaded. Any code which requires you to retain an
object being passed as a parameter to prevent it from being
deallocated by another thread is completely broken. So broken that you
cannot save it. Retaining at the beginning of a method and releasing
at the end *will not save you* in all circumstances. If a second
thread can destroy an object in the middle of your method, it can very
well destroy it at the beginning before your code has a chance to
retain the thing. This is almost certainly not happening in the case
in question, because the Cocoa code is almost certainly not utterly
broken as this behavior would imply. If it *is* utterly broken in this
way, then you cannot fix it with band-aids such as you describe. All
you can do is file a bug and avoid using this method at all costs.
> Now that might be something like
> desiredURL = [link retain] which or it might be just a [link retain] at the
> start of the method.
This is a pointless placebo which at best will do nothing and at worst
will deceive you into thinking that you've accomplished something
useful.
> Of course, if threads are the culprit then things are going to be entirely
> more complicated as it's possible for the variable to be sent -release at
> points between method invocations - are you using threads yourself
> explicitly (i.e. not spawned by the system frameworks)?
It's certainly possible for this to happen, but if the code is written
correctly then it is *im*possible for any such release messages to
result in the destruction of any locally referenced objects.
> But yes, generally,
> you should retain any object that's being passed into a method to ensure
> that it stays around for the lifetime of that method and release them when
> you exit.
Absolutely false. Never do this. If you find yourself doing this then
it means you've either horribly misunderstood something or you've
encountered a severe bug. The correct course of action is to either
correct the misunderstanding or correct the bug, not to wrap all of
your methods in retain/release calls.
> In some cases, you can guarantee that the object won't receive a
> -release (for instance, when you're explicitly locking to guarantee that the
> code happens sequentially) but even then it is more of an optimization that
> is quite likely to break if your code is restructured than anything else.
> However, at this point, I wouldn't worry about optimizations such as that.
> There are better things to optimize than message passing overhead.
Sounds like you may be confusing -release with -dealloc. It's true
that objects may be sent -release from other threads. But that doesn't
matter unless the release results in deallocation of the object. And
if that happens you have a severe bug which you must fix.
I have never wrapped any methods in retain/release calls as you
described. I've never seen code that does it, and I've never even
*heard* of anyone doing it until now. Ask yourself this: do you do
[self retain] at the beginning of a method and [self release] at the
end? If not, why not? Self is just a parameter like any other. It gets
no special treatment (aside from being used to make decisions during
the dispatch process). If what you're saying is correct, you must also
do [self retain] and [self release] at the beginning and end of every
single method you ever write. I hope that this is self-evidently
absurd.
Mike
_______________________________________________
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