• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to completely release every object allocated when opening a NIB file ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to completely release every object allocated when opening a NIB file ?


  • Subject: Re: How to completely release every object allocated when opening a NIB file ?
  • From: Dustin Voss <email@hidden>
  • Date: Fri, 16 Jan 2004 18:14:51 -0800

On 13 Jan, 2004, at 9:15 PM, mmalcolm crawford wrote:

On Jan 13, 2004, at 8:12 PM, Charles Srstka wrote:

Which brings me to the point, isn't this badly broken? The point of Cocoa's reference counting was that you were supposed to only have to release something if you specifically called init or retain on it. As it is, the rule has to be extended to: "You only need to release something if you called init or retain on it. Except for top-level objects in nib files, which you own and have to release, even though you didn't call retain or init, even though your code might not even *know* about those objects, and even though the nib file could be edited after you write the code and your code could have to deal with objects that didn't *exist* at the time the code was written. Oh, and forget this rule for NSDocument's nib file, which *will* release the objects, causing you to crash if you try to release them. So don't release those! But do release everything in everything other nib file. Wait, unless you use a NSWindowController, because *it* will release the nib file objects... but oh, Cocoa's memory management is easy!"

I think you're being overly dramatic. The situation can be explained much more simply. Let's start with the documentation:

"When you load a nib file, all its top level objects have a reference count of one. When the objects are no longer needed, another object needs to release them. That other object is usually the files owner.
Note that NSWindowController automatically releases its nib files objects for you. Also, NSWindow has a isReleasedOnClose attribute, which automatically releases the window for you when its closed."
<file:///Developer/Documentation/Cocoa/Conceptual/MemoryMgmt/ index.html>

What isn't documented is how this interacts with NSNib. Do NSNib-loaded objects need to be released by another object? Is it retaining the top-level objects upon loading the NIB or instantiating the NIB?

[snipped simple situations]

For other more complex situations: If your nib file is controlled by a window controller, as is recommended, it takes care of memory management for you. And for other configurations, recall, "NSWindow has a isReleasedOnClose attribute, which automatically releases the window for you when its closed.

So if you have a top-level NSWindow, with the isReleasedOnClose attribute set, it does not need to be released with the other top-level objects, and if you use a released-on-close window with a window controller, I guess you get a bus error. This is adding even more inconsistency. I think I read somewhere that they don't recommend setting this attribute, and this is why.

For me, the complexity comes in when I have a NIB that does not contain a window. I have one that contains only a view. I instantiate it mutiple times (using NSNib for performance) and add it to the window. I auto-release it immediately, to account for the reference count of one I get after instantiating (not loading) it, but I add it to the window before the auto-release pool gets emptied.

I wanted to release it in the view controller's dealloc method (the view controller owns the NIB of the view), but this caused bus errors when I release the window or the view controller. Why? I have no idea. There is some sort of interaction which I either didn't see or isn't documented.

Now I'm worried about leaking the view -- I'm no longer sure how it gets cleaned up. Plus, it'll be really awkward to add a controller or something to the view's NIB, since I'd need to free it without freeing the view. How do I time that without causing dangling pointers everywhere?

So things typically have to become reasonably complicated before you generally have to worry about memory management issues, and these are unlikely to be situations to confront a newcomer.

True. But the NIB stuff still needs to be rationalized. Is there some reason the top-level objects can't just be auto-released? Let the file's owner retain the objects it is interested in immediately after loading the NIB, and let the rest of the objects be retained by the NIB's internal network of connections.
_______________________________________________
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.
References: 
 >Re: How to completely release every object allocated when opening a NIB file ? (From: Charles Srstka <email@hidden>)
 >Re: How to completely release every object allocated when opening a NIB file ? (From: j o a r <email@hidden>)
 >Re: How to completely release every object allocated when opening a NIB file ? (From: Charles Srstka <email@hidden>)
 >Re: How to completely release every object allocated when opening a NIB file ? (From: mmalcolm crawford <email@hidden>)

  • Prev by Date: Re: Cocoa equivalent to TK canvas widget
  • Next by Date: Re: Is my computer connected to the web?
  • Previous by thread: Re: How to completely release every object allocated when opening a NIB file ?
  • Next by thread: Re: How to completely release every object allocated when opening a NIB file ?
  • Index(es):
    • Date
    • Thread