tags:

views:

60

answers:

2

object release without pool-just leaking?? is this memory leak??? if yes how can i avoid this???

+1  A: 

is this memory leak???

Yes.

if yes how can i avoid this???

You need an NSAutoreleasePool. For example,

int main () {
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

  ... your main code ...

  [pool drain];
  return 0;
}
KennyTM
Im call various functions in the main?? should i create different pools for each function??? or one pool in the main will do???
Pradeep Kumar
One pool is enough. (Unless you want to release some memory fast.)
KennyTM
Keep in mind, that `NSApplicationMain` creates an autorelease pool for you.
Georg
im new to cocoa?nsapplicationmain creates autorelease pool, so do all the variables i use in the "-awakefornib" and -Event handling functions will be autoreleased???In my main() im creating a object of "Class A" in which im calling nsapplicationmain().And im also createing a thread in the "Class A" (class created in the main)...
Pradeep Kumar
i think im confusing.....ill post the code..
Pradeep Kumar
You are doing it wrong (no worries, we were all new at one time or another); you shouldn't be creating threads by hand and the need to create objects in main() is an advanced patterns. I suggest you step back from StackOverflow and focus your time on the Objective-C, Cocoa, and Memory Management Guides offered at http://developer.apple.com/
bbum
Thanks guys.....solved it......
Pradeep Kumar
+2  A: 

The object isn't being released, it's being autoreleased. Autorelease is like deferred cleanup, it means "I'm done with this, feel free to get rid of it later". Sometimes another part of the code, like a function that yours was called from, will pick up the object (retain it) before the autorelease occurs.

Autoreleasing is managed by a class called NSAutoreleasePool. The class maintains a stack of pools, and when an object is autoreleased, it is added to the topmost pool on the stack. Draining a pool causes it to release all of the objects that have been added to it - and if any have been released enough, they will be deallocated.

There are two approaches you can take to remove this message. One, which works on both the Mac and iPhone, is the one suggested by KennyTM - add an autorelease pool around you code that uses autoreleased objects. You need one for each thread where you autorelease Cocoa objects, more is possible but usually one is sufficient. The other alternative, though only if you're targeting Mac OS, is to enable garbage collection. That won't solve all of your problems but does mean that unreferenced objects are automatically removed.

If you're not familiar with retain counts and autorelease pools, you may have other memory management problems in your code. I recommend you use the static analyser on your project (choose "Build & Analyze" from Xcode's build menu) and address any issues it raises.

Graham Lee