Iphone UIImage&;UIImageView内存泄漏
我正在仪器上测试我的应用程序, 我看到我的UIImage和UIImageView正在疯狂地泄漏内存 我基本上使用递归,所以相同的变量在每次调用中加载不同的图像Iphone UIImage&;UIImageView内存泄漏,iphone,Iphone,我正在仪器上测试我的应用程序, 我看到我的UIImage和UIImageView正在疯狂地泄漏内存 我基本上使用递归,所以相同的变量在每次调用中加载不同的图像 nextImageName = [[NSString alloc] init]; nextImageName2 = [[NSString alloc] init]; nextImageName = [[currentPlayers objectAtIndex:playerIndex] retain]; ne
nextImageName = [[NSString alloc] init];
nextImageName2 = [[NSString alloc] init];
nextImageName = [[currentPlayers objectAtIndex:playerIndex] retain];
nextImageName2 = [[currentPlayers objectAtIndex:(playerIndex+1)] retain];
nextImage = [[UIImage alloc] init];
nextImage2 = [[UIImage alloc] init];
nextImage = [UIImage imageNamed:nextImageName];
nextImage2 = [UIImage imageNamed:nextImageName2];
nextImageView = [[UIImageView alloc] init];
nextImageView2 = [[UIImageView alloc] init];
nextImageView = [[UIImageView alloc] initWithImage:nextImage];
nextImageView2 = [[UIImageView alloc] initWithImage:nextImage2];
NSLog(@"r:%d",currentRound);
NSLog(@"%d vs. %d", playerIndex, playerIndex+1);
buttonOne = [[UIButton alloc] init];
buttonTwo = [[UIButton alloc] init];
playerOne = nextImageView;
playerTwo = nextImageView2;
playerOne.frame = CGRectMake(180.0, 200.0, 275.0, 275.0);
playerTwo.frame = CGRectMake(550.0, 200, 275.0, 275.0);
buttonOne.frame = CGRectMake(180.0, 200.0, 275.0, 275.0);
buttonTwo.frame = CGRectMake(550.0, 200.0, 275.0, 275.0);
[buttonOne addTarget:self action:@selector(announceWinner:)
forControlEvents:UIControlEventTouchUpInside];
[buttonTwo addTarget:self action:@selector(announceWinner2:)
forControlEvents:UIControlEventTouchUpInside];
谁能帮帮我吗?这快把我逼疯了
我最初在dealloc上发布了所有变量,但它似乎没有进入dealloc,所以我也把它放在了viewDidUnload和DIRECEIVEMEMORYWARNING中。我认为问题在于: “我基本上使用递归,因此相同的变量可以在每次调用中加载不同的图像” …再加上: “我最初在dealloc上发布了所有变量,但它似乎没有进入dealloc,所以我也将它放在了viewDidUnload和DIRECEIVEMEMORYWARNING中” 因此,从本质上讲,如果我正确理解了您的代码,您在类的生命周期中多次通过alloc/init部分,但只有在类本身被释放时调用
release
一次。我想那会像疯了一样泄露出去
您应该能够通过将alloc/init部分更改为以下模式来修复它:
if (nextImageName) {
//if it was previously set, release it so that the old instance doesn't leak
[nextImageName release];
}
nextImageName = [[NSString alloc] init];
if (nextImageName2) {
[nextImageName2 release];
}
nextImageName2 = [[NSString alloc] init];
//and so on...
这假设这些变量都在类上声明为实例变量,并且在init
中,您将它们都设置为nil
,如下所示:
- (void) init {
if ((self = [super init])) {
//set default values
nextImageName = nil;
nextImageName2 = nil;
//and so on...
//do other setup things here
}
}
这是你的第一个memleak。。您正在分配一个字符串,然后用数组中的字符串替换该对象
您只需删除第一行即可。。在从数组中获取对象之前,不必分配对象
但是如果没有看到更多的代码,我们就无法看到您稍后发布的内容。。我希望您稍后会发布内容;) 您正在分配和初始化每个对象,然后再次增加保留计数 无论何时调用alloc、copy、new或retain…对象的retain计数都会增加1。释放时,保留计数下降1。所以当你打字的时候
nextImageName = [NSString alloc] init];
然后
nextImageName = [[currentPlayers objectAtIndex:playerIndex] retain];
nextImageName现在的保留计数为2。如果你只使用第二行,你会没事的。您不需要分配新对象,只需要保留返回给您的对象
同样,nextImageView和nextImageView2分配了两次,因此保留计数为2。你只需要打第二个电话
nextImageView = [UIImageView alloc] initWithImage:nextImage];
希望这有帮助。试试这段代码。它将清除UIImage和UIImageView上的内存泄漏
nextImageName = [NSString string];
nextImageName2 = [NSString string];
nextImageName = [[currentPlayers objectAtIndex:playerIndex] retain];
nextImageName2 = [[currentPlayers objectAtIndex:(playerIndex+1)] retain];
nextImage = [UIImage imageNamed:nextImageName];
nextImage2 = [UIImage imageNamed:nextImageName2];
nextImageView = [[UIImageView alloc] initWithImage:nextImage];
nextImageView2 = [[UIImageView alloc] initWithImage:nextImage2];
NSLog(@"r:%d",currentRound);
NSLog(@"%d vs. %d", playerIndex, playerIndex+1);
从技术上讲,这并不完全正确。nextImageName上的保留计数仍然为1。如果您尝试发布两次,应用程序将崩溃。发生的事情是,他丢失了旧的nextImageName对象,无法找回它。。所以它被泄露了。我明白你在说什么了。谢谢你指出这一点!还在学习…你是对的!我分配了很多次,只发布了一次。。。塔克斯!你可能还想把巴斯蒂安的答案写在下面。他也提出了一个合理的观点。
nextImageName = [NSString string];
nextImageName2 = [NSString string];
nextImageName = [[currentPlayers objectAtIndex:playerIndex] retain];
nextImageName2 = [[currentPlayers objectAtIndex:(playerIndex+1)] retain];
nextImage = [UIImage imageNamed:nextImageName];
nextImage2 = [UIImage imageNamed:nextImageName2];
nextImageView = [[UIImageView alloc] initWithImage:nextImage];
nextImageView2 = [[UIImageView alloc] initWithImage:nextImage2];
NSLog(@"r:%d",currentRound);
NSLog(@"%d vs. %d", playerIndex, playerIndex+1);