Hi all,
in dealloc funcn do we need to release even local class variables other than releasing properties.
Always release local objects. You are at the mercy of a garbage collector if you do not release your items and classically, a garbage collector waits for a "timeout" period for a memory allocation before collecting it whether it is in use or not and that period is VERY long. This can easily be considered a memory leak.
This is assuming you even have a garbage collector in your language/framework version. If not, the allocation never gets released until you reboot.
Modern garbage collectors are much more intelligent than your description and there is typically no long delay before collection on a dead chunk of memory.
In any case, if your app leaks the memory is returned as soon as you close the app. A reboot is way overkill.
If you close an app all memory it had is immediately taken up by the OS. There is no delay, and there is no garbage collector "waiting just a bit longer" to reclaim the memory.
Is this Mac OS or Windows as well? I must be out of the loop then. I am primarily a Windows developer.
As I recall from reading the Apple developer documentation, if garbage collection is enabled, the release method doesn't do anything, so calling it will not change anything. Code that was written to do manual memory management will still run just fine when the garbage collection is turned on, but the Cocoa framework will simply ignore release calls and do its own garbage-collection thing.
If garbage collection is turned off, then release will work as expected.
I've been working for years developing applications with languages with garbage collection (Java, C#, Objective-C), and I've never seen a case where the garbage collection mechanism was too slow or left too much memory uncollected. No doubt an application that allocates and releases a gig of memory hundreds of times a second would have a problem with garbage collection not collecting before it runs out of memory, but 99.999% of applications don't do anything like that.
Worry about garbage collection if it becomes a problem; otherwise, just trust that it will do its job.
[[[obj alloc] init] autorelease]
I thought I read in the new Hillegrass book that you had to instaniate the object in a manner to use the garbage collector. In that case you are right as it doesn't care about the "release" statement. Don't you have to?Code:[[[obj alloc] init] autorelease]
Reference counting
If you use garbage collection, the methods that are used to implement the manual reference counting system (retain, release, dealloc, autorelease, and retainCount) have no effectthe Objective-C messenger short circuits their dispatch. As a result, overriding release and dealloc is not supported when garbage collection is enabledthis makes obsolete some object caching patterns.
Note, however, that CFRetain and CFRelease do still have an effect in Core Foundation objects. See also Adopting Garbage Collection.
This is every single OS in existence. A closed app relinquishes its resources back to the OS. Now. Not later. It isn't a matter of the app wanting to, it is OS enforced.
Although to quickly answer the OP's question: Yes. Unless you are using the Objective-C garbage collector, you are required to release all your object's instance variables. This is why there is a dealloc function to begin with.
Not completely correct.
@interface DemoClass : NSObject {
int aDemoVar;
}
.
.
.
@end
aDemoVar is an instance variable, and it is not one you need to deallocate.