• 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: [Newbie Q] Memory Management in Cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Newbie Q] Memory Management in Cocoa


  • Subject: Re: [Newbie Q] Memory Management in Cocoa
  • From: Ondra Cada <email@hidden>
  • Date: Tue, 5 Nov 2002 23:01:02 +0100

On Tuesday, November 5, 2002, at 07:22 , Flemming Bengtsson wrote:

I'm getting a little bit confused about when to dealloc an instance.

Never. It is dealloc'd automatically for you when (and if) the last of its users releases it.

It's obvious that instances created like [[foo alloc] init ]; should be
deallocated when done with.

Well, in a sense, yes, but not deallocated. They should be _released_ when done with in case you keep them for long -- from a practical POV, if you store them into an instance or a global variable:

// long-time pattern
-init... {
...
ivarOrGlobal=[[Foo alloc] init];
...
}
-(void)dealloc {
...
[ivarOrGlobal release];
...
}

If you place them into an automatic variable or into another container or "into" an accessor, they should be _auto_released immediately:

{ // short-time pattern
id local=[[[Foo alloc] init] autorelease];
...
}

// long-time patterns, where the object's lifespan is controlled by another code: from your POV, *exactly the same* as a short-time pattern
[array addObject:[[[Foo alloc] init] autorelease]];
[self setThisOrThat:[[[Foo alloc] init] autorelease]];

But what about instances created like:
NSString * aNSString = [NSString stringWithCString:"Hello" length:5];

In this case, you did not create the instance. Some other one (in this case, the library code) did. Therefore, some other one is also responsible to release it -- you aren't. Therefore, short-time pattern and long-time ones where a different code controls object lifespan are extremely plain:

{ // short-time pattern
id local=[Foo fooWithWhatever...];
...
}

// long-time patterns, object's lifespan is controlled by another code
[array addObject:[Foo fooWithWhatever...]];
[self setThisOrThat:[Foo fooWithWhatever...]];

On the other hand, in this case (more precisely: whenever you get any object from any method but alloc/init pair, copy, or mutableCopy, or somewhat obsoleted new) you have to retain (and later release) the object if you want to keep it for longer by yourself (again, practically it means "in a global or instance variable"):

// long-time pattern
-init... {
...
ivarOrGlobal=[[Foo fooWithWhatever...] retain];
...
}
-(void)dealloc {
...
[ivarOrGlobal release];
...
}

Okay? There are some more sophisticated memory management tricks and gotchas, but you would lose nothing if you stick with the above simple rules for awhile.
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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.
  • Prev by Date: Re: Cocoa equivalent to Javadoc?
  • Next by Date: Re: [Newbie Q] Memory Management in Cocoa
  • Previous by thread: Re: [Newbie Q] Memory Management in Cocoa
  • Next by thread: Re: [Newbie Q] Memory Management in Cocoa
  • Index(es):
    • Date
    • Thread