Re: Managing Retain/Release for a ContentView
Re: Managing Retain/Release for a ContentView
- Subject: Re: Managing Retain/Release for a ContentView
- From: Dietmar Planitzer <email@hidden>
- Date: Tue, 12 Aug 2003 20:52:48 +0200
On Tuesday, August 12, 2003, at 07:24 PM, Joel Lachter wrote:
I have an application where the documents have an additional "helper"
window, and I need a little advice on how best to release the view of
that window.
This is what I do currently. First I create a window:
exptWindow = [[ExptWindow alloc]
initWithFrame:NSMakeRect(64,64,800,600)];
Then I create a view for that window (because I use alloc I get a
retain count of 1 for this view):
pmView = [[PM1View alloc] initWithFrame:[exptWindow frame]];
Then I add the view to the window (somewhat to my surprise this
increments the retain count to 2; I couldn't find that in the
documentation but if I send pmView a retainCount message it goes up
to 2 after this call):
This behavior is to be expected, even if it is not explicitly mentioned
in the documentation of -setContentView, because its defined by
convention.
In Cocoa, de facto every method which takes an object as an argument is
supposed to retain or copy it. More or less the only exception to this
rule is -setDelegate: which does not retain the passed in object.
[exptWindow setContentView: pmView];
All of these calls are within my subclass of NSDocument. Here is the
question, should I be thinking of the second retain as belonging to
my document or the window?
Window. The window has created a so called 'strong reference' to your
view by retaining it. The reason why the window retained your view is
because it wouldn't be really funny for the window if the newly set
content view would suddenly disappeared behind its back because i.e.
you released your view somewhere else in your code. In order to avoid
this problem, it retains the view.
The document already owns one reference to
pmView. I originally had the document send pmView a release message
before closing the window, it seems a little silly to be sending it
two. On the other hand, it seems strange to me to send a release
message in the window's dealloc method since the window did not
create the view. What is the correct style for this?
The window didn't create the view, but it can't exist without it, ergo
it keeps a strong reference to it around as long as it exists.
Normally you could instantly release your reference to the view after
you invoked -setContentView: because the window will in any case retain
the view. I.e.:
[exptWindow setContentView: pmView];
[pmView release];
`pmView' has now turned into a weak reference because it still points
to your view on one hand, but no longer owns it on the other hand.
Naturally this makes only sense if the lifetime of your view is
supposed to be exactly as long as the lifetime of the window. Otherwise
you must keep your own strong reference to the view.
Regards,
Dietmar Planitzer
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.