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

littlealmo

macrumors newbie
Original poster
Sep 13, 2010
4
0
Hi all,

I am running the following code which reads a simple csv file and then creates an object for each line and sets one value. When I run the code in instruments using leaks it creates a leak (NSCFString) for each line in the csv. I cannot understand how to stop this leak?


Code:
- (void)viewDidLoad {
   
	 [super viewDidLoad];
	 buildingsArray  = [[NSMutableArray alloc] init];
	
	//BUILDINGS
	NSBundle *bundle = [NSBundle mainBundle];
	NSString *fileContents = [NSString stringWithContentsOfFile:
					[bundle pathForResource:@"buildings" ofType:@"csv"]];
	NSMutableArray *quizArray = [[NSMutableArray alloc] 
					initWithArray:[fileContents componentsSeparatedByString:@"\n"]];	

	for (int i = 0; i < [quizArray count]; i++) {				   
		NSMutableArray *chunks = [[NSMutableArray alloc] 
					initWithArray:[[quizArray objectAtIndex:i]  componentsSeparatedByString:@","]];
				
		Building *a = [[Building alloc]init];
		
		//the problem shows up here
		a.name = [chunks objectAtIndex: 0];
		[buildingsArray addObject:a];
		
		[a release];
		[chunks release];
	}
	[quizArray release];
}

The leak shows up when I do
Code:
a.name = [chunks objectAtIndex: 0];
if I replace this line with
Code:
a.name = @"test";
then there is no leak?
 
viewDidLoad can be called more than once, but buildingsArray never gets released and is not using a setter property, so each time viewDidLoad gets called you leak memory.
 
hi jnic,

Thanks for the reply I am using viewDidLoad just to highlight the issue I have put the code into a UITableView within a navigation view, I call the table view once and then go back one level in the navigation. I have set this up just to test this problem in leaks. The buildingsArray is released in dealloc. So even when i call viewDidLoad only once then leave I see the memory leak. In the building object I have set the "name" as
Code:
@property(nonatomic, copy) NSString *name;
. So I am not sure why I get the leaking of strings?
 
here is the leaks screenshot generated for each loop through
Code:
a.name = [chunks objectAtIndex: 0];
 

Attachments

  • Screen shot 2010-09-13 at 12.49.00.png
    Screen shot 2010-09-13 at 12.49.00.png
    240.7 KB · Views: 133
You're allocating a Building object, setting the name property to a string, and then finally releasing the Building object. Have you made sure that you're releasing the name property in the dealloc method of the Building class?
 
Fixed

Hi bredell,

You where exactly right I was not releasing the name property in the Building object. I added the code and the leak has gone away. Thanks for your help with this it was driving me mad. I did not realise that object dealloc method has to release there properties. Anyway thanks again for taking the time to look at this.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.