1

Hey, I am making a cocoa touch static library, And I have this problem:

I am running my project in the simulator with the Leaks instrument, And I am coming up with leaks for autoreleased objects.

I know for a fact that I have at least one NSAutoreleasePool in place at a time (in my main() method), my question is, how often should I put in others (I am developing for iPhone and iPad if that matters)

UPDATE: I have figured out that, for some reason, my code isn't exiting out of the UIApplicationMain() call on iOS 4, I am just getting a SIGKILL signal, and my autorelease pool isn't draining. How can I fix that (I mean the app getting a SIGKILL)

Thanks

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 1
    I do it in every selector that gets called in another thread. – ySgPjx Nov 05 '10 at 11:58
  • When developing OSX apps in XCode, the console window fills with warnings should one ever call an objective c function that results in an allocation and there is no NSAutoreleasePool in place. Do the UIKit targets not do this? – Chris Becke Nov 05 '10 at 12:05
  • Oh no they do, but I have an autorelease pool, so no warnings in the console, only leaks in instruments... – Richard J. Ross III Nov 05 '10 at 12:16
  • see http://stackoverflow.com/questions/3098684/iphone-app-running-simulator-4-0-received-sigkill for info about sigkill – Vladimir Nov 05 '10 at 12:40

3 Answers3

2

NSAutoreleasePool is required when you run something in a background thread, so if your functions can be run in a background then you need to create a autorelease pool in them:

- (void) willRunInBackground{
   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
   ...
   [pool drain];
}

The second situation where NSAutoreleasePool will be useful is when you create many autoreleased objects in a loop - to avoid to much autoreleased objects hanging around you can create and drain autorelease pool on loop iteration (as Joe mentioned).

But you memory leaks are likely caused by the 1st reason - each thread must have its own NSAutoreleasePool to handle autoreleased objects.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • 1
    May be your code is wrong and objects are not (auto)released properly? usually if there's problem with no nsautoreleasepool you get console message "autoreleased with no pool in place - just leaking" – Vladimir Nov 05 '10 at 12:06
  • then check your code - may be there's an memory management error? try to build your project with static analyzer (build&analyze option) - it may point you to problematic code. – Vladimir Nov 05 '10 at 12:10
  • I use that all the time, as I haven't been using objective-c for that long, and I fixed all of that stuff.... if you cant help, thats OK, I just thought you might be able to help... ill keep trying.. odly, In my app, it never reaches the place where I return an integer in my `main()` method... gonna check that out – Richard J. Ross III Nov 05 '10 at 12:13
1

The fact that you are autoreleasing objects does not, in itself, prevent a memory leak. Since you're not seeing messages in Console telling your that your objects are being autoreleased outside a pool, it indicates that the problem isn't that they're not being put into a pool.

You must not be managing your retain count properly. Remember that all calls to -alloc and -copy must be balanced by calls to -release or -autorelease. Perhaps you aren't releasing your member variables in a class's dealloc method somewhere. Start by using Instruments to find where you are allocating / copying your objects, then look at every place you retain and release them to ensure each object's retain count is balanced.

Ryan
  • 16,626
  • 2
  • 23
  • 20
0

From the WWDC videos standard practice holds that a tight loop with a lot of variables flying around is a good place to put one. Start it before the loop, everything in the loop that is autoreleased should go to that pool, and drain it afterward.

fearmint
  • 5,276
  • 2
  • 33
  • 45