analyze and memory alerts in xcode

I ran 'analyze" in xcode on a current iOS project to try to track down a freeze issue and there are a number of memory alerts that I don't understand (screenshot of one below).

What is going on there: I have a custom ObjC class extending NSObject; in the init method I alloc/init an NSMutableArray and then in a loop, populate it with NSMutableArrays. This nested array is declared as a property and released in dealloc(). It lives for the life of the app.

Am I doing this wrong? I don't understand the alert#3: @ object not referenced in this execution path and has a retain count of +1.

Since my class allocs the outer array, it owns it and will clean it up. Do the inner arrays need to be released?

Thanks for any tips - still new at this.

EDIT/ADDITION Trying to stamp out the additional memory warnings I am getting so I thought I would add to the question here in the event someone stumbles upon this w/ the same issue.

I am getting the following alert with the code below (the 2nd line "[keyArray addObject: etc"). What is going on: I have a custom class (Key - based on NSObject) that I instance and store in an array. Based on answers to my previous question, I guess my alloc increases the retain count and then when it is added to the array, the retain count isn't decremented - so the memory warning occurs.

What is the proper way to handle something like this? Use a placeholder like this:

Key * k = [[Key alloc] initKeyWithPath:path isBlackKey:NO]];
[keyArray addObject: k];
[k release];

Is that the proper way to do it? Or is there I way to write the custom class to return an autoreleased obj? (thanks and sorry to be so long winded!).

Potential leak of an object allocated on line 460 Method returns an Objective-C object with a +1 retain count (owning reference) Object allocated on line 460 is not referenced later in this execution path and has a retain count of +1 (object leaked)

-(void) addOctaveToArraysWithTransform:(CGAffineTransform*)trans andPath: (CGMutablePathRef) path
{
    path = [self createCF_keyUsingTransform: trans];
    [keyArray addObject:[[Key alloc] initKeyWithPath:path isBlackKey:NO]];
}

Key.h

#import <Foundation/Foundation.h>
#import "Key.h"

@interface Key : NSObject {
    @public
    CGMutablePathRef keyPath;
    BOOL isBlackKey;
    NSValue * path;
    int keyState;
    BOOL needsRedraw;
 }

 @property (nonatomic, assign) int keyState;
 @property (nonatomic, assign) BOOL needsRedraw;
 @property (nonatomic) CGMutablePathRef keyPath;

 -(id) initKeyWithPath:(CGMutablePathRef) aPath isBlackKey:(BOOL)flag;
 -(CGMutablePathRef) getKeyPath;

@end

Key.m

#import "Key.h"

@implementation Key

@synthesize keyState, needsRedraw, keyPath;

-(id) initKeyWithPath:(CGMutablePathRef) aPath isBlackKey:(BOOL)flag
{
    if ((self = [super init])){
         isBlackKey = flag;
         keyState = 0;
         needsRedraw = NO;
         keyPath = aPath;
         CGPathRetain(keyPath);
 }
return self;
}

-(CGMutablePathRef) getKeyPath
{
    return  keyPath;
}
@end

Answers


You have an allocation of an NSMutableArray on each iteration of the for-loop. Instead use: NSMutableArray array] which is a convenience method that return an autoreleased NSMUtableArray suitable for adding to fieldNotes which will retain the NSMutableArray.


Yes, you have to release the inner arrays to balance the alloc/init. Remember the outer array will retain each inner array, and the outer array will presumably release those later. But here you are still responsible for the alloc/init you just did.

Hope that helps.


Need Your Help

Does this PHP code open up a website to SQL Injection

php sql security sql-injection

I'm working on a web app and I came across this code snippit

Need the best way to iterate a file returning batches of lines as XML

scala iterator list-comprehension for-comprehension

I'm looking for the best way to process a file in which, based on the contents, i combine certain lines into XML and return the XML.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.