【发布时间】:2011-11-02 06:36:15
【问题描述】:
我试图消除应用程序中的内存泄漏,但我发现在 for 循环中使用 alloc NSbundle 时会导致内存泄漏。
这是我的代码:-
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 是另一个带有 .xib 的类,我正在尝试将其加载到 myFeed 类中。
但是如果在循环结束时我添加[grid release] 然后可以看到 myGrid 的视图,但 myGrid 中的 UiwebView 不显示其数据。
并且仪器还在该行显示内存泄漏:
myGrid *grid = [[myGrid alloc]initWithNibName:@"myGrid" bundle:[NSBundle mainBundle]];
我在这里做错了什么......
【问题讨论】:
-
你在哪里插入
[grid release]?很有必要 -
我已经添加了[网格发布]代码
标签: cocoa-touch xcode4 ios4