Hi all, i'm fairly new to Cocoa and need help with memory leak. I was testing a very simple application. There is only one button and when the button is clicked , it calls buttonClicked method then calls populateArray method. It looks like this.
- (IBAction) buttonClicked: (id) sender
{
//g_array is a public variable
//[g_array release]; // still leaks
g_array = [[NSMutableArray alloc] init];
[self populateArray];
}
- (void) populateArray
{
int i;
for (i = 0; i < 10; i++)
{
NSString *str = [NSString stringWithFormat: @"%i", i];
//NSString str = [[NSString alloc] initWithString"test"];
[g_array addObject: str];
//[str release];
}
}
- (void)dealloc
{
// Release Array
[g_array release];
}
If i click the button first few times, it doesn't leak. But after several clicks, it leaks. What would be the proper way to reuse public NSMutableArray variable? Should initialization of array go into awakeFromNIB instead? Thanks.
- (IBAction) buttonClicked: (id) sender
{
//g_array is a public variable
//[g_array release]; // still leaks
g_array = [[NSMutableArray alloc] init];
[self populateArray];
}
- (void) populateArray
{
int i;
for (i = 0; i < 10; i++)
{
NSString *str = [NSString stringWithFormat: @"%i", i];
//NSString str = [[NSString alloc] initWithString"test"];
[g_array addObject: str];
//[str release];
}
}
- (void)dealloc
{
// Release Array
[g_array release];
}
If i click the button first few times, it doesn't leak. But after several clicks, it leaks. What would be the proper way to reuse public NSMutableArray variable? Should initialization of array go into awakeFromNIB instead? Thanks.