Using Leaks tools and an Alloc object: Are auto-implemented objects counted as leaks?

I have an iPhone app that receives memory alerts, so I'm trying to find leaks, use memory more efficiently, etc. using tools. Among other things, I'm trying to pull out any objects with auto-implementation and replace alloc / init / release with manual objects. However, some API calls do not have an β€œinit” version (see code below). I have some basic misunderstandings:

  • If I β€œcall” the API and return essentially to auto-implemented objects, can these objects appear as leaks in the Tools? It seems that I see this behavior in Tools.

  • If so, should I just ignore it if there is no alternative to non-autorelease, and am I using the API that I need? Also, if this code comes out a lot, should I completely rethink the algorithm?

Here is some utility code from my application that got a lot of names. Basically determines whether two dates are significantly "equal." I left comments in the code so that you can see the types of improvements that I will make in my code base. this DID reduces memory leaks when it was subsequently run in Tools , when I started manually creating NSDate (and release), which helped. However, I still have date component objects that seem to leak ... but this is an API call (sorry for formatting the code, but I can't improve it on SO):

+ (BOOL)isDayEqualToDay:(NSDate*)date anotherDate:(NSDate*)anotherDate { NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; //NSCalendar *cal; NSDateComponents *componentsFromDate, *componentsFromAnotherDate; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; //cal = [NSCalendar currentCalendar]; componentsFromDate = [cal components:unitFlags fromDate:date]; componentsFromAnotherDate = [cal components:unitFlags fromDate:anotherDate]; BOOL bDatesEqual = ([componentsFromDate year] == [componentsFromAnotherDate year] && [componentsFromDate month] == [componentsFromAnotherDate month] && [componentsFromDate day] == [componentsFromAnotherDate day]); [cal release]; return bDatesEqual; /* return ( [componentsFromDate year] == [componentsFromAnotherDate year] && [componentsFromDate month] == [componentsFromAnotherDate month] && [componentsFromDate day] == [componentsFromAnotherDate day] );*/ } 

I think the components FromDate and componentsFromAnotherDate are displayed as leaks, but there are only objects essentially returned from the NSData API call (autoreleased). Not sure what else I could do to make it more efficient, and I ask myself the question of how best to use the tools. Suggestions?

+6
memory-leaks objective-c iphone cocoa-touch instruments
source share
2 answers

An authorized object should not appear as a memory leak. Sometimes APIs have memory leaks inside them. You must file a bug report using apple. New classes such as NSCalendar and NSDateComponenets are especially suspicious.

Regarding saving vs autorelease, the general rule is that it doesn't matter if you are not in a closed loop. In this case, if a hard loop goes many thousands of times without an event, this means that you never "clear" the auto resource pool.

+4
source share

When using things like GCD, there is an autostart pool, but you cannot find out when (if ever) the autostart pool is merged by default. If you are sure that atorealized objects are not being released, make sure that you understand that you are using threading api. If the memory serves me correctly, GCD calls (dispatch_async) will sort the abstract pools for you, BUT the actual pool depletion can take a lot of time. NSOperations, on the other hand, allows you to create auto-detection pools.

I have seen a memory leak detection in the Tools based on 10 second intervals, leading to false warnings about a memory leak due to the long delay before the automatic release pool was deleted. So try wrapping the violation code in:

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; ... [your code] ... [pool drain]; 

I would recommend not trying to replace all auto-implementers with manual versions. Using autorelease results in a balance calculation with saving / freeing in one place. Creating an object and then automatically realizing it immediately prevents many memory errors, in my opinion. Quick, easy. You will forget to release material when making manual calls. Errors when performing manual releases are especially dangerous.

Adhering to the pool itself gives you more control and during intensive work with resources it can sometimes be useful to create and merge your own pools. But, as always, try and check, do not make any assumptions.

+3
source share

All Articles