I was trying to remove the memory leaks in my app but i found out that while using the alloc NSbundle in a for loop it is giving me memory leak.
here is my code:-
in my class myFeed.m
for(int i = 0; i<20; i++)
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSDictionary *temp = (NSDictionary*)[topData objectAtIndex:i];
NSString *iconName = [temp objectForKey:@"feed_source"];
NSString *imageName;
TimeLineGrid *grid = [[TimeLineGrid alloc]initWithNibName:@"TimeLineGrid" bundle:[NSBundle mainBundle]];
CGRect frame = grid.view.frame;
if([iconName isEqualToString:@"Youtube"])
imageName = [[NSBundle mainBundle]pathForResource:@"Youtube" ofType:@"png"];;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
frame.size=CGSizeMake(130, 130);
}
else
{
frame.size = CGSizeMake(180, 180);
}
[grid setDelegate:self];
if (isConditionTrue)
{
x = x - 200 - topFactor;
}
else
x = x - frame.size.width;
if (x < 0)
{
x = 0;
}
frame.origin.x = x;
frame.origin.y = y;
grid.view.frame = frame;
grid.view.tag = tagCount;
if ([temp objectForKey:@"feed_item_nid"])
{
[tagToNIDMap addObject:[temp objectForKey:@"feed_item_nid"]];
}
if ([(NSString*)[temp objectForKey:@"feed_source"]isEqualToString:@"Youtube"])
{
[grid.webView loadHTMLString:[self getYoutubeCode:(NSString*)[temp objectForKey:@"feed_item_url"] :grid.webView.frame] baseURL:nil];
}
else
{
NSString * str = [[WebServiceController Initialize]parseHTMLData:[temp objectForKey:@"feed_item_body"] : @"//img"];
NSURL * url = [NSURL URLWithString:str];
NSURLRequest * req = [NSURLRequest requestWithURL:url];
[grid.webView loadRequest:req];
}
//[grid.view addSubview:grid.webView];
tagCount++;
[grid setHome:frame];
grid.dict = (NSDictionary*)[topData objectAtIndex:i];
// grid.icon.frame=CGRectMake(grid.view.frame.origin.x, grid.view.frame.origin.y-20,grid.icon.frame.size.width,grid.icon.frame.size.height)
grid.icon.image = [UIImage imageNamed:imageName];
grid.detail.text = [temp objectForKey:@"feed_item_title"];
[scroll1 addSubview:grid.view];
[imageName release];
[grid release];
[pool release];
}
}
myGrid is an another class with a .xib which i am trying to load in myFeed class.
but if at the end of the loop i add [grid release] then the view of myGrid can be seen but the UiwebView present in myGrid does not show its data.
And the instrument is also showing memory leak at the line :
myGrid *grid = [[myGrid alloc]initWithNibName:@"myGrid" bundle:[NSBundle mainBundle]];
what i am doing wrong here…
You aren’t doing anything with all those
myGridobjects (class names should start with a capital letter, by the way). Your loop creates a view controller, loads something into it’swebViewproperty, and then releases it, which is pointless.I assume you are doing something with
gridafter this loop has finished – what is that? If you really need 20 differentmyGridobjects then you need to add them to an array or something during your loop, because at the moment you are just overwriting the value ofgridevery time your loop processes. If you remove the release statement then you will have a singlemyGridobject at the end of the loop, and 19 leaked instances from the other passes.If you want a single
gridobject then create it before the loop begins – but it looks like that would just end up with a single object that is configured however you set it during the last iteration of the loop.UPDATE
OK, now you’ve included your full code it is a bit clearer. You need to keep around each
gridobject. Create an NSMutableArray before your for loop (this should probably be a property of whatever object this code is in):Within the loop, before you release grid, add the following:
This prevents you losing the reference to each
gridobject except the last one, and stops you leaking memory in this code. You can later release or modify the objects in yourgridArrayas you see fit.