views:

106

answers:

2

The following code in my function (run in a loop) is causing my program to use more and more memory until it crashes. What am I doing wrong?

- (void) processTrackValues:(NSMutableArray*) tags {  
    NSImage* trackArt = [tags objectAtIndex:5];  
    NSMutableArray* tempArtArray = [[NSMutableArray alloc] init];  
    [tempArtArray addObject:trackArt];  
    [tempArtArray release];  
}

I also tried:

- (void) processTrackValues:(NSMutableArray*) tags {  
    NSImage* trackArt = [tags objectAtIndex:5];  
    NSMutableArray* tempArtArray = [[NSMutableArray alloc] init];  
    [tempArtArray addObject:trackArt];  
    [trackArt release];  
    [tempArtArray release];  
}

Edit: Here is more information on the surrounding code. I have also added more code to the sample for a bigger picture.

  • trackArt is an NSImage pointer to one of the arguments to this function.
  • The NSImage object that trackArt points to is created outside of this function.
  • I am allocating and releasing tempArtArray each iteration of the loop (since the function is called for each iteration of the loop)
A: 

Hi Chetan, can you post the surrounding loop code to help get a bigger picture?

Do you allocate and release tempArtArray for every loop iteration? If so can you instead allocate it once outside of the loop and reuse it?

How are the trackArt objects created?

EddieCatflap
Agreed, post more of the loop.
Rui Pacheco
I have posted more of the surrounding loop and added edits to the question with more information. Hope this helps.
Chetan
I don't see a loop there, only a method definition.
Rui Pacheco
+2  A: 

The (now posted twice) method is nonsense. At the end of the method, the method has accomplished exactly nothing.

Post the real code.

In particular, how are you actually creating the NSImage instances? How do you know that this particular method is causing the bloat and eventual crash?

bbum