[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: strange crasher that puzzles me
From: |
hns |
Subject: |
Re: strange crasher that puzzles me |
Date: |
Tue, 31 Mar 2009 08:49:50 -0700 (PDT) |
User-agent: |
G2/1.0 |
> Are there any easy rules of thumb to follow, any good documentation about
> memory-management in objective-c for the "beginner" like me?
Yes:
1. only +alloc, +copy, +mutableCopy, +new return objects that you must
release/autorelease explicitly (unless you want to keep it until
application exit, e.g. a global object).
2. all (well, 99,5%) other methods (convenience allocators, getters,
transformers, ...) do not need to be released (unless see below)
3. init does not change anything
4. if you retain you must also release/autorelease explicitly later
5. you must retain if you want that the pointer is valid until after
beyond the end of your current method
6. release does invalidate a pointer immediately, autorelease allows
you to still use the pointer until you return from your current method
(but not beyond that point)
That's it. Should be a big popup in every Obj-C development tool :-)
So:
1. {
localPtr=[class alloc/copy/new];
...
[localPtr (auto)release];
}
2. {
localPtr=[obj method];
...
### no release ###
}
3. {
extPtr=[obj retain];
}
...
{
[extPtr (auto)release];
}
extPtr is instance variable or global variable.
if 1+3 are combined the release/retain is not required.
Nikolaus