简体   繁体   中英

does initWithNibName need to be released

I use initWithNibName to load detail views. I was just thinking do these things need to be released at all? init is basically adding a retain count of 1?

Yes. Read this section . Anything starting with init... gives you an object you own.

@Mel:

Yes you need to release them.

A Part from Apple's Doumentation:

You own any object you create

You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).

But as such it has nothing to do with initWithNibName but it is related to four words which allocate the memory that is 'mutableCopy','copy', 'retain' and 'alloc'. So hope it is clear to you now.

init keyword just initializes an object. Allocation of memory is done though alloc or retain or copy or mutableCopy keyword

And the retain count of 1 that you are talking about is because of the alloc keyword, not the initWithNibName .

Hope this helps you.

The section in question:

You own any object you create You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).

You can take ownership of an object using retain A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. [...]

When you no longer need it, you must relinquish ownership of an object you own You relinquish ownership of an object by sending it a release message or an autorelease message. In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object.

You must not relinquish ownership of an object you do not own This is just corollary of the previous policy rules, stated explicitly.

Generally, though, you should avoid thinking in terms of retain counts and focus on ownership . If you own it, it's up to you to release it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM