Memory Management in Objective-C when dealing with NSMutableArray - iphone

In Instruments the tempPlayer object is showing a leak. In this code, in every for loop I keep on allocating a new tempPlayer instance, set its playerCode variable with a string and add it to an NSMutableArray in each iteration. Instruments shows me a leak in the alloc statement. Is there any way to prevent tat leak in the tempPlayer object ?
for(int i = 0 ; i < [homeLineupArray count] ; i++) {
NSArray * tildeSeperator = [[homeLineupArray objectAtIndex:i] componentsSeparatedByString:#"~"];
[self.tempPlayer release];
self.tempPlayer = [[LineUpsPlayer alloc] init];
tempPlayer.playerCode = [tildeSeperator objectAtIndex:0];
[matchLineUp.homeTeamPlayingEleven addObject:tempPlayer ];
}
Thanks
Harikant Jammi

I would simply do this.
for(int i = 0 ; i < [homeLineupArray count] ; i++) {
NSArray *tildeSeperator = [[homeLineupArray objectAtIndex:i] componentsSeparatedByString:#"~"];
LineUpsPLayer *player = [[[LineUpsPlayer alloc] init] autorelease];
player.playerCode = [tildeSeperator objectAtIndex:0];
[matchLineUp.homeTeamPlayingEleven addObject:player];
}
You can also replace your loop with this:
for (NSString *lineup in homeLineupArray) {
NSArray *tildeSeparator = [lineup componentsSeparatedByString:#"~"];
...
}
You don't usually want to save each item while iterating through an array to an instance variable since it keeps changing and you only reference it in the method.

Rather than using the property, tempPlayer, use a local variable, and release after adding to the array:
for(int i = 0 ; i < [homeLineupArray count] ; i++) {
NSArray * tildeSeperator = [[homeLineupArray objectAtIndex:i] componentsSeparatedByString:#"~"];
LineUpsPlayer* tempPlayer = [[LineUpsPlayer alloc] init];
tempPlayer.playerCode = [tildeSeperator objectAtIndex:0];
[matchLineUp.homeTeamPlayingEleven addObject:tempPlayer ];
[tempPlayer release];
}
The array retains the objects you add - that's why you need to release the temporary object too.

Things may depend on how do you declare your tempPlayer property in your class (and as it looks that it is temporary object, consider do you need a property accessor for it?)
for(int i = 0 ; i < [homeLineupArray count] ; i++) {
NSArray * tildeSeperator = [[homeLineupArray objectAtIndex:i] componentsSeparatedByString:#"~"];
[self.tempPlayer release]; // Decrease retain count
self.tempPlayer = [[LineUpsPlayer alloc] init]; // retain count increase by 1 or 2
tempPlayer.playerCode = [tildeSeperator objectAtIndex:0];
[matchLineUp.homeTeamPlayingEleven addObject:tempPlayer]; // retain count increase
}
So as you can see you retain your object more times then you release it so it eventually leaks. Probably your code may be rewritten this way:
for(int i = 0 ; i < [homeLineupArray count] ; i++) {
NSArray * tildeSeperator = [[homeLineupArray objectAtIndex:i] componentsSeparatedByString:#"~"];
LineUpsPlayer* tempPlayer = [[LineUpsPlayer alloc] init]; // object's retain count is 1
tempPlayer.playerCode = [tildeSeperator objectAtIndex:0];
[matchLineUp.homeTeamPlayingEleven addObject:tempPlayer]; // container takes ownership of the object
[tempPlayer release]; // we do not need to own this object as it is in container now
}

As said above, I normally use a local variable to populate the mutable array. In other cases Instruments didnt show me any leak. So is this the right way to go about ?
for(int i =0 ; i < ([arrayOfHomePlayers count]); i++ ) //creating home players detials object
{
localString = [NSMutableString string];
localString = [arrayOfHomePlayers objectAtIndex:i] ;
NSArray * localPlayersArray = [localString componentsSeparatedByString:#"~"] ;
localPlayerPosition = [ [PlayerPosition alloc] init] ;
NSArray * playerNameArray = [[localPlayersArray objectAtIndex:0] componentsSeparatedByString:#" "] ;
localPlayerPosition.globalID = [localPlayersArray objectAtIndex:1];
[(teamFormationDetails.homePlayerList) addObject:localPlayerPosition] ;
[localPlayerPosition release] ;

First, you could change the release to an autorelease and move it to the line after the self is assigned.
Second, the addobject is adding it to a collection which retains it, and I don't see you removing it from that collection, so that's your 'leak'. However, I'm not sure there even is a leak, if you intended to leave it in the collection.

Related

iphone: addObjectsFromArray not adding after reinitializing nsmutable array

i have the following code
inAppKeys = [[MKStoreManager sharedManager] purchasableObjectsDescription ];
NSMutableArray * unremovableArray = [[NSMutableArray alloc] init];
for(int i = 0; i<[inAppKeys count]; i++){
for (int j=0; j< [categories count]; j++) {
NSString * inAppKey = [[categories objectAtIndex:j] valueForKey:#"inAppKey"];
if([inAppKey isEqualToString: [inAppKeys objectAtIndex:i]]){
[unremovableArray addObject:[categories objectAtIndex:j]];
}
}
}
categories = [[NSMutableArray alloc] init];
[categories addObjectsFromArray:unremovableArray];
where categories is nsmutablearray .. the thing is addObjectsFromArray leave the categories empty .. what do i do wrong?
Looks to me like you're referring to [categories count] and [categories objectAtIndex:j] before you even alloc/init categories.
Having re-read your title ("reinitializing") which suggests you've previously inited categories, I'm now assuming that you have a master set of categories that you're trying to reduce to the ones actually purchased. If so, I wouldn't re-use the variable "categories" as that's confusing. (I assume categories was auto-released, or else you've got a leak). How 'bout using unremovableArray instead of leaking it?
I'd also use fast enumerators for clarity and speed...
NSLog(#"categories: %#", categories);
inAppKeys = [[MKStoreManager sharedManager] purchasableObjectsDescription ];
NSLog(#"inAppKeys:%#", inAppKeys);
NSMutableArray * unremovableCategories = [[NSMutableArray alloc] init];
for(NSString* thisAppKey in inAppKeys) {
for (NSDictionary* thisCategory in categories) {
if ([[thisCategory valueForKey:#"inAppKey"] isEqualToString: thisAppKey]){
[unremovableCategories addObject:thisCategory];
break; //having added this category; no reason to continue looking at it
}
}
}
//now use unremovableCategories...

execute addObject, but NSMutableArray has no object

I try to use to codes below to add object to NSMutableArray
NSMutableArray* multipartData;
- (void)processDataChunk:(NSData *)postDataChunk
{
if (!postHeaderOK)
{
UInt16 separatorBytes = 0x0A0D;
NSData* separatorData = [NSData dataWithBytes:&separatorBytes length:2];
for (int i = 0; i < [postDataChunk length] - l; i++)
{
NSRange searchRange = {i, l};
if ([[postDataChunk subdataWithRange:searchRange] isEqualToData:separatorData])
{
NSRange newDataRange = {dataStartIndex, i - dataStartIndex};
dataStartIndex = i + l;
i += l - 1;
NSData *newData = [postDataChunk subdataWithRange:newDataRange];
if ([newData length])
{
[multipartData addObject:newData]; //A:set break point here
}
}
}
}
}
I set breakpoint at A:, and found that newData is not nil.
What is wrong with my codes?
Welcome any comment
You declare multipartData but do not allocate or initialize it. Somewhere before adding objects you must have
NSMutableArray *multipartData = [[NSMutableArray alloc] init];
or possibly
NSMutableArray *multipartData = [[[NSMutableArray alloc] init] autorelease];
as your needs dictate. But you must allocate and initialize multipartData before adding to or accessing anything in it.

Problem adding custom objects to Mutable Array

quick question regarding Array's in xcode. I have ht efollowing code, which is supposed to go through an array of strings which it has got through php and JSON, and trun these strings into a custom object with the strings as the ivars for the object then add that object to a new array:
for (int i = 0; i<[list count]; i++) {
Article *article = [[Article alloc] init]; //creates custom object
article.uid = [[list objectAtIndex:i] objectAtIndex:0];
article.title = [[list objectAtIndex:i] objectAtIndex:1]; //adds string as ivars
article.description = [[list objectAtIndex:i] objectAtIndex:2];
articleArray = [[NSMutableArray alloc] init]; //inits the new array
[articleArray addObject:article]; //should add the object but seems to fail
[article release]; //releases the object
NSLog(#"%#", article.description);
}
NSLog(#"%d", [articleArray count]);
NSLog([articleArray description]);
}
The code does return the correct values using NSLog(#"%#", article.description); but not the correct length for the new array and it only adds one value to the array which is the string for article.description which makes no sense to me. The list array contains 2 elements each of which are arrays in themselves containing the strings.
You're recreating the articleArray in every loop. Declarate it outside, and it will work:
NSMutableArray *articleArray = [[NSMutableArray alloc] init]; //inits the new array
for (int i = 0; i<[list count]; i++) {
Article *article = [[Article alloc] init]; //creates custom object
article.uid = [[list objectAtIndex:i] objectAtIndex:0];
article.title = [[list objectAtIndex:i] objectAtIndex:1]; //adds string as ivars
article.description = [[list objectAtIndex:i] objectAtIndex:2];
[articleArray addObject:article]; //should add the object but seems to fail
[article release]; //releases the object
NSLog(#"%#", article.description);
}
NSLog(#"%d", [articleArray count]);
NSLog([articleArray description]);
}
You also may want to use the nicer for(NSArray *listElement in list) syntax instead.

NSArray objects go out of scope after returning pointer

I am attempting to use the below code in a function to return an array of dictionary objects. Unfortunately, after the return to the next function in the stack all of the rows in the mutable array have become 'out of scope'. From my understanding, the array should retain the row (dictionary) object automatically so even after the return, where the row pointer goes out of scope, the row objects should still have a retain count of 1. What am I doing wrong here? How do I build this array in such a way that the objects it contains don't get released?
for (int i = 1; i < nRows; i++)
{
NSMutableDictionary* row = [[[NSMutableDictionary alloc] initWithCapacity:nColumns] ];
for(int j = 0; j < nColumns; j++)
{
NSString* key = [[NSString stringWithUTF8String:azResult[j]] ];
NSString* value = [[NSString stringWithUTF8String:azResult[(i*nColumns)+j]] ];
[row setValue:value forKey:key];
}
[dataTable addObject:row];
}
return dataTable;
This line:
NSMutableDictionary* row = [[NSMutableDictionary alloc] initWithCapacity:nColumns] ];
should use the autorelease:
NSMutableDictionary* row = [[[NSMutableDictionary alloc] initWithCapacity:nColumns] ] autorelease];
From what i understand:
-(NSMutableArray*) getArrayOfDictionaries{
int nRows=somenumber;
int nColumns=someOthernumber;
char **azResult=someArrayOfStrings;
NSMutableArray *dataTable=[[NSMutableArray alloc] init];
for (int i = 1; i < nRows; i++)
{
NSMutableDictionary* row = [[[NSMutableDictionary alloc] initWithCapacity:nColumns]];
for(int j = 0; j < nColumns; j++)
{
NSString* key = [[NSString stringWithUTF8String:azResult[j]] ];
NSString* value = [[NSString stringWithUTF8String:azResult[(i*nColumns)+j]] ];
[row setValue:value forKey:key];
}
[dataTable addObject:row];
//you should add the following line to avoid leaking
[row release];
}
//watch for leaks
return [dataTable autorelease];
//beyond this point dataTable will be out of scope
}
-(void) callingMethod {
//dataTable is out of scope here, you should look into arrayOfDictionaries variable
NSMutableArray* arrayOfDictionaries=[self getArrayOfDictionaries];
}
You should look into the local variable in callingMethod instead of dataTable which is local to the method I called getArrayOfDictionaries

NSArray to NSMutableArray as random stack

Just a conceptual description first:
I am reading input from a text file (a list of words) and putting these words into an NSArray using componentsSeparatedByString method. This works.
But I wanted to select the words randomly and then delete them from the array so as to ensure a different word each time. Of course, you cannot change the NSArray contents. So...
I copied the contents of the NSArray into an NSMutableArray and use IT for the selection source. This also works - 269 objects in each array.
To return a word from the NSMutableArray I use the following code:
note- the arrays are declared globally
as
arrMutTextWords = [[NSMutableArray alloc] init]; //stack for words
arrTextWords = [[NSArray alloc] init]; //permanent store for words
-(NSString*) getaTextWord
{
// if the mutable text word array is empty refill
if ([arrMutTextWords count] == 0){
for (int i = 0 ; i < [arrTextWords count]; i++)
[arrMutTextWords addObject:[arrTextWords objectAtIndex:i]];
}
int i = random() % [arrMutTextWords count];
NSString* ptrWord = [arrMutTextWords objectAtIndex:i];
[arrMutTextWords removeObjectAtIndex:i];
return ptrWord;
}
The program crashes during a call to the method above - here is the calling code:
arrTmp is declared globally arrTmp = [[NSArray alloc] init]; //tmp store for words
for (int i = 0 ; i < 4; i++) {
tmpWord = [self getaTextWord];
[arrTmp addObject:tmpWord];
[arrTmp addObject:tmpWord];
}
I'm thinking that somehow deleting strings from arrMutTextWords is invalidating the NSArray - but I can't think how this would occur.
One possible source for problems is your fetching AND removing the NSString object from your list. Removing it releases that NSString instance therefore devalidating your reference.
To be shure to retain a reference you should use this code sequence instead:
NSString * ptrWord = [[[arrMutTextWords objectAtIndex:i] retain] autorelease];
[arrMutTextWords removeObjectAtIndex:i];
return ptrWord;
By the way: You should use
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray: array];
instead of copying all values by hand. While i do not know the implementation of NSMutableArray, i know from times long ago (NeXTstep), that there are several possible optimizations that may speed up basic NSArray operations.
And finally copying this way is much more concise.
Just ran this through XCode and got random words returned, however I skipped the whole for loop and used addObjectsFromArrayfrom NSMutableArray.
NSArray *randomArray = [[NSArray alloc] initWithObjects:#"Paul", #"George", #"John", nil];
NSMutableArray *muteArray = [[NSMutableArray alloc] init];
[muteArray addObjectsFromArray:randomArray];
int i = random() % [muteArray count];
NSString* ptrWord = [muteArray objectAtIndex:i];
[muteArray removeObjectAtIndex:i];
NSLog(#"ptrWord %#", ptrWord); //gave me a different name each time I ran the function.
Hope this clears some things up.