Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

atmc

macrumors regular
Original poster
Jun 28, 2009
177
62
Porto, Portugal
Hi,

I'm having a problem while developing for iOS. I can't find why I have a memory leak.

Code:
NSDictionary *feriadosDictionary = [NSDictionary dictionaryWithContentsOfFile:file];
	

NSArray *feriadosArray = [feriadosDictionary objectForKey:@"xpto"];
for (int i = 0; i != [feriadosArray count]; i++) {
		NSArray *ferArr = [[[NSArray alloc] init] autorelease];
		ferArr = [feriadosArray objectAtIndex:i];
		
		
		
		if ([ferArr count] == 6) {
			
				Feriado *feriado = [[[Feriado alloc] init] autorelease];

		[feriado setData: [ferArr objectAtIndex:0]];

//...
}

If I change this last line to, for example

Code:
[feriado setData: @"something"];

there is no memory leak discovered. I understand that the problem may be with ferArr array, but I can't figure out exactly why.

Can anyone help me?

Thanks in advance.
 
Last edited:
From what I can tell, your code snippet makes no sense.

You are not checking that the dictionary actually received the data.
The feriadosArray variable isn't allocated.
You are not checking that feriadosArray was assigned any data.
You are trying to place only one one item into feriadosArray, so the for loop isn't needed.
Use of a ferArr array also makes no sense since the first has at most one item in it.
The test of the count of the ferArr array for 6 would never be met given the other two have at most 1 item.
Etc.

So asking for help for this sample is unlikely to get you a helpful response. Post the real code.

One thing to try is the "Build and Analyze" selection under the Build menu. If it identifies leaks, that will help you to figure the issue. I'll leave it to your to learn how to use that tool. If you have a current paid up Apple iPhone developer account, look at getting some videos from the WWDC2010. I think the videos of interest would be in the 300 series of talks.
 
Ok, that's the complete code:

Code:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	//declared in .h
	feriadosMunicipio = [[NSMutableArray alloc] init];
	
	NSString *file = [[NSBundle mainBundle] pathForResource:@"xpto1" ofType:@"plist"];
	
	
	NSDictionary *feriadosDictionary = [[NSDictionary alloc] initWithContentsOfFile:file];
	
	if (feriadosDictionary) {
		
		NSArray *feriadosArray = [[NSArray alloc] initWithArray:[feriadosDictionary objectForKey:@"xpto"]];
	
	
		for (int i = 0; i != [feriadosArray count]; i++) {
			NSArray *ferArr = [[NSArray alloc] initWithArray:[feriadosArray objectAtIndex:i]];
			
			
			Feriado *feriado = [[Feriado alloc] init];

			[feriado setData:[ferArr objectAtIndex:0]];
			[feriado setTipo:[ferArr objectAtIndex:1]];
			[feriado setNome:[ferArr objectAtIndex:2]];
			[feriado setDescricao:[ferArr objectAtIndex:3]];
			[feriado setMunicipiosArr:[ferArr objectAtIndex:5]];
		
			NSArray *arr_aux = [feriado municipiosArr];
		
			for (int j = 0; j != [arr_aux count]; j++) {
				if ([[arr_aux objectAtIndex:j] isEqualToString:municipio]) {
					[feriadosMunicipio addObject:feriado];
				}
			}
			
			[ferArr release];
			[feriado release];

		}
		
		[feriadosArray release];
	}
	
	[feriadosDictionary release];
	
	
	
    [super viewDidLoad];
}

My Feriado class
Code:
#import <Foundation/Foundation.h>


@interface Feriado : NSObject {

	NSString *data;
	NSString *nome;
	NSString *link;
	NSString *descricao;
	NSString *tipo;
	NSMutableArray *municipiosArr;
}

@property(nonatomic, copy) NSString *data;
@property(nonatomic, copy) NSString *nome;
@property(nonatomic, copy) NSString *descricao;
@property(nonatomic, copy) NSString *link;
@property(nonatomic, copy) NSString *tipo;
@property(nonatomic, retain) NSMutableArray *municipiosArr;

@end

Basically, if I change [feriado setData:[ferArr objectAtIndex:0]]; and so on to [feriado setData:mad:"something"]; there is no memory leak anymore.

I've tried build and analyze and nothings shows up.

What I am trying to do is just parse a plist file, with this structure:

Code:
Dictionary
     xpto
          array
               string
               string
               string
               string
               string
               array
                     string, string, string, string

Inside xpto, I can have as many items (arrays) as it is necessary. Inside each array, the last position is an array with more items.
 
Last edited:
In your implementation file for Feriado, are you releasing your ivars in a dealloc method? Like this;

Code:
- (void) dealloc
{
  [data release];
  [nome release];
  [link release];
  [descricao release];
  [tipo release];
  [municipiosArr release];

  [super dealloc];
}
 
In your implementation file for Feriado, are you releasing your ivars in a dealloc method? Like this;

Code:
- (void) dealloc
{
  [data release];
  [nome release];
  [link release];
  [descricao release];
  [tipo release];
  [municipiosArr release];

  [super dealloc];
}

Thanks a lot. That really helped. I completely forgot of adding this into my implementation file of class Feriado.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.