Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSA自动释放池泄漏_Iphone_Objective C - Fatal编程技术网

Iphone NSA自动释放池泄漏

Iphone NSA自动释放池泄漏,iphone,objective-c,Iphone,Objective C,我知道这一定是我忽略了的一些简单的事情,但为什么会泄漏: //add a pool for this since it is on its own thread NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //add the image NSURL * imageURL = [NSURL URLWithString:[recipeData objectForKey:@"imagePath"]]; NSData * im

我知道这一定是我忽略了的一些简单的事情,但为什么会泄漏:

//add a pool for this since it is on its own thread
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//add the image
NSURL * imageURL = [NSURL URLWithString:[recipeData objectForKey:@"imagePath"]];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];

//resize to make it fit the screen space 
CGRect frame = myImageView.frame;
frame.size.width = 320;
frame.size.height = 357;
myImageView.frame = frame;

[self.view addSubview:myImageView];


[activity stopAnimating];

[pool drain];
[self placeItems];
我得到一个错误:

_NSAutoreleaseNoPool():类NSPathStore2的对象0x4e2fdf0在没有池的情况下自动释放-只是泄漏

我试着移动[泳池排水沟]的位置,但没有任何效果。我在谷歌搜索原因时看到了很多类似的代码


感谢您的帮助。

由于无法保留自动释放池,因此排空池会产生释放并随后释放池的效果。我怀疑placeItems(或在
[pool drain]
之后调用的其他地方)中一定需要一个自动释放池,因为此时池可能已经不存在了


因此,您可能希望尝试注释“排放”消息,看看这是否会使泄漏消失。

排放池的效果是释放并随后释放它,因为自动释放池无法保留。我怀疑placeItems(或在
[pool drain]
之后调用的其他地方)中一定需要一个自动释放池,因为此时池可能已经不存在了


因此,您可能希望尝试对排放消息进行注释,看看这是否会使泄漏消失。

这里有很多话要说:

  • 首先,您正在泄漏
    myImageView
    。您必须在
    -addSubview
    之后释放它
  • 接下来,因为您在另一个线程上,所以您的[pool-drain]必须位于末尾
  • 最后,由于您不在主线程上,因此无法执行任何UI操作。尝试将
    [self.view addSubview:myImageView]
    替换为
    [self.view performselector或onmainthread:@selector(addSubview:)with object:myImageView waituntldone:YES]
    。与
    [活动停止动画制作]
    相同

正如Brian所说,
-drain
消息必须在线程的末尾。

这里有很多话要说:

  • 首先,您正在泄漏
    myImageView
    。您必须在
    -addSubview
    之后释放它
  • 接下来,因为您在另一个线程上,所以您的[pool-drain]必须位于末尾
  • 最后,由于您不在主线程上,因此无法执行任何UI操作。尝试将
    [self.view addSubview:myImageView]
    替换为
    [self.view performselector或onmainthread:@selector(addSubview:)with object:myImageView waituntldone:YES]
    。与
    [活动停止动画制作]
    相同

正如Brian所说,
-drain
消息必须在线程的末尾。

您正在泄漏
myImageView
(需要释放)。除此之外,乍一看一切正常。您正在泄漏myImageView(需要发布)。除此之外,乍一看一切都很好,这就解决了问题。我只是得到一个警告“unusedvariablepool”,我同意一切,除了关于注释掉池漏的部分。排空自动释放池应该始终是该方法的最后一行。我只是建议将排空消息注释掉,作为解决问题的快速方法-下一步将是为其找到更好的位置。正如你所提议的,这个方法的最后一行将是这样一个地方的候选:)我把它移到了最后,这个方法的最后一行,一切都很好。这就解决了它。我只是得到一个警告“unusedvariablepool”,我同意一切,除了关于注释掉池漏的部分。排空自动释放池应该始终是该方法的最后一行。我只是建议将排空消息注释掉,作为解决问题的快速方法-下一步将是为其找到更好的位置。正如你所提议的,这个方法的最后一行将是这样一个地方的候选:)我把它移到了最后,这个方法的最后一行,一切都很好。