释放对象后的内存问题
NSArray *imageExtension = [info.ThemeImage componentsSeparatedByString:@"."];
NSString *path = [[NSBundle mainBundle] pathForResource:[imageExtension objectAtIndex:0]ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];
image=[image addImageReflection:0.50];
[CarouselView setFrame:CGRectMake(0,-200, image.size.width, image.size.height)];
UIButton *button = [[UIButton alloc]init];
[button setFrame:CGRectMake(0,0, image.size.width, image.size.height)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
释放对象图像后,我仍然存在内存泄漏...
我不知道为什么它在仪器泄漏中显示内存泄漏
NSArray *imageExtension = [info.ThemeImage componentsSeparatedByString:@"."];
NSString *path = [[NSBundle mainBundle] pathForResource:[imageExtension objectAtIndex:0]ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];
image=[image addImageReflection:0.50];
[CarouselView setFrame:CGRectMake(0,-200, image.size.width, image.size.height)];
UIButton *button = [[UIButton alloc]init];
[button setFrame:CGRectMake(0,0, image.size.width, image.size.height)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
After releasing the object image still i do have memory leak...
I dont have any idea why its showing memory leak in instruments leak
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
上面的代码存在三个问题,会导致内存泄漏。
首先,您在此处创建
图像
:然后将该指针
图像
分配给其他内容,从而丢失原始引用:addImageReflection
我假设给了您一个自动释放的对象。尽管如此,您稍后还是释放了
image
:这里释放的不是您分配的原始指针,而是后来分配的自动释放对象。所以你通过过度释放造成了第二个问题。
最后,还有第三个问题。您在此处创建的对象
button
永远不会被释放:There's three issues with the code above that would cause a memory leak.
First, you create the
image
here:You then assign that pointer
image
to something else, thereby losing the original reference:addImageReflection
I assume gives you an autoreleased object.Despite this, you later release
image
:What you are releasing here is not the original pointer you allocated, but instead the later assigned autoreleased object. So you are creating a second problem by over releasing.
Finally, you have a third problem. The object
button
that you create here is never released:你没有松开按钮?如果上面的代码在一个函数中并且被多次调用,按钮就会泄漏。
尝试添加
you didn't release the button? If the code above is in a function and it was called many time button will leak.
try add
做完之后也一样吗?
Is it same after you do?
在这里,您正在分配一个新的 UIImage
在这里,您正在对图像执行一些操作(反射?),并且该函数可能会返回一个自动释放的实例。在这里,您正在将该实例重新分配给原始实例变量。您将失去对原始图像的引用实例,它将被泄漏..此外,您还引用了一个自动释放的实例
,您随后错误地尝试释放该实例,这将导致崩溃..
Here you are allocing a new UIImage
Here you are doing some action(reflection?) on image and probably this function returns an autoreleased instance..Here you are reassigning that instance to your original instance variable..You will lose reference to your original image instance and it will be leaked..Also you are having reference to an autoreleased instance
which you wrongly try to release afterwards, which will cause the crash..