释放对象后的内存问题

发布于 2024-12-10 21:04:12 字数 650 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾其所爱 2024-12-17 21:04:12

上面的代码存在三个问题,会导致内存泄漏。

首先,您在此处创建图像

UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];

然后将该指针图像分配给其他内容,从而丢失原始引用:

image=[image addImageReflection:0.50];

addImageReflection我假设给了您一个自动释放的对象。

尽管如此,您稍后还是释放了image

[image release]; 

这里释放的不是您分配的原始指针,而是后来分配的自动释放对象。所以你通过过度释放造成了第二个问题。

最后,还有第三个问题。您在此处创建的对象button永远不会被释放:

UIButton *button = [[UIButton alloc]init];

There's three issues with the code above that would cause a memory leak.

First, you create theimagehere:

UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];

You then assign that pointerimageto something else, thereby losing the original reference:

image=[image addImageReflection:0.50];

addImageReflection I assume gives you an autoreleased object.

Despite this, you later releaseimage:

[image release]; 

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 objectbuttonthat you create here is never released:

UIButton *button = [[UIButton alloc]init];
落墨 2024-12-17 21:04:12

你没有松开按钮?如果上面的代码在一个函数中并且被多次调用,按钮就会泄漏。
尝试添加

[button release];

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

[button release];
心清如水 2024-12-17 21:04:12

做完之后也一样吗?

[[self view] addSubview:button];
[button release];

Is it same after you do?

[[self view] addSubview:button];
[button release];
狠疯拽 2024-12-17 21:04:12

在这里,您正在分配一个新的 UIImage

UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];

在这里,您正在对图像执行一些操作(反射?),并且该函数可能会返回一个自动释放的实例。在这里,您正在将该实例重新分配给原始实例变量。您将失去对原始图像的引用实例,它将被泄漏..此外,您还引用了一个自动释放的实例

image=[image addImageReflection:0.50];

,您随后错误地尝试释放该实例,这将导致崩溃..

[image release];

Here you are allocing a new UIImage

UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];

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

image=[image addImageReflection:0.50];

which you wrongly try to release afterwards, which will cause the crash..

[image release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文