Breaking News
Home / iOS / iOS Isuses / Autoreleased with no pool in place – just leaking

Autoreleased with no pool in place – just leaking

Sometimes during iOS development, you see a an error like that in your logs.

011-05-07 17:38:03.820 ContactShark[7789:5d03] *** __NSAutoreleaseNoPool(): Object 0x6157710 of class NSCFString autoreleased with no pool in place – just leaking
2011-05-07 17:38:03.825 ContactShark[7789:5d03] *** __NSAutoreleaseNoPool(): Object 0x6127f70 of class NSCFString autoreleased with no pool in place – just leaking
2011-05-07 17:38:03.826 ContactShark[7789:5d03] *** __NSAutoreleaseNoPool(): Object 0x6160970 of class NSPathStore2 autoreleased with no pool in place – just leaking

The Code

This error normally occurs when we try to execute code asynchronously by executing a seprate thread using the detachNewThreadSelector method. 

  1.  [NSThread detachNewThreadSelector:@selector(queryJigsawPoints) toTarget:self withObject:nil];  
  2.  
  3. (void) queryJigsawPoints
  4. {
  5.     debugLog(@"queryJigsawPoints");
  6.  
  7.  
  8.     int points = [JigsawHelper getPointsForLoggedInUser];
  9.     AppCache *cache = [AppCache getInstance];
  10.     [cache put:[NSNumber numberWithInt:points] forKey:JIGSAW_POINTS];
  11. }

Answer

The key here to make sure is all the code this is execute inside the method is wrapped inside an NSAutoreleasePool. This auto release pool will take care of automatically releasing the not required resources, if not present it will cause a leak resulting in the above error. The following code corrects the leak issue.

  1.  [NSThread detachNewThreadSelector:@selector(queryJigsawPoints) toTarget:self withObject:nil];  
  2.  
  3. (void) queryJigsawPoints
  4. {
  5.    
  6.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  7.     debugLog(@"queryJigsawPoints");
  8.  
  9.  
  10.     int points = [JigsawHelper getPointsForLoggedInUser];
  11.     AppCache *cache = [AppCache getInstance];
  12.     [cache put:[NSNumber numberWithInt:points] forKey:JIGSAW_POINTS];
  13.    
  14.     [pool release];  
  15.  
  16. }
[pb_builder]

Check Also

Signing Identities, Missing Private Key, Cannot sign App

Issue Cannot sign the App in XCode, says Missing Private Key. <!

Advertisment ad adsense adlogger