ios [UIImage class]: message sent to deallocated instance

        NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:mnuPicPath]];

            UIImage *img = [UIImage imageWithData:data];
            NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingFormat:@"/%@.jpg",mnu.menuNumber];
            NSLog(@"uniquePath:%@",uniquePath);
            [UIImageJPEGRepresentation(img, 1) writeToFile:uniquePath atomically:YES];
            NSLog(@"img.retainCount:%d",[img retainCount]);
            [img release];    //[UIImage class]: message sent to deallocated instance
            [data release];  //NSConcrete data]:message sent to deallocated instance


*** -[UIImage class]: message sent to deallocated instance

http://stackoverflow.com/questions/2507145/whats-the-correct-way-to-alloc-a-uiimage-to-memory-and-release-it-iphone

Using Instruments, I keep on getting pointed to a memory leak with a UIImage.
I think I'm assigning and releasing the memory correctly. The leaked object in instruments is described as NSConcreteData

Is the following the correct way to assign and release a UIImage?

UIImage* flagimg = [UIImage imageWithData: [NSData dataWithContentsOfURL:url2]];    
[flagimg release];
flagimg =nil;
share | improve this question
  

up vote 5 down vote accepted

[UIImage imageWithData:] returns an autoreleased object, which should not be released by you again. So this code snipped contains not a memory leak but the opposite, a double free (in the worst case).

Note that Instruments sometimes generates false positives and/or reports memory leaks in the Foundation itself (yep, they make mistakes too :-).

The fastest way to alloc/release an object is to avoid convenience initializers (like imageWithData:) and instead to something like

NSData* data = [[NSData alloc] initWithContentsOfURL:url]];
UIImage* img = [[UIImage alloc] initWithData:data];
[data release];
// use your image
[img release];

This will allocate and release your object right away and not wait until the autorelease pool is cleaned.

But please note too, that a memory leak is generally not memory that is not yet freed, but that is lost and cannot be freed anymore, so an object which will be deallocated by the autorelease pool is not considered a memory leak.

share | improve this answer
 
Whats the consequence of a "double release" ? –  dubbeat Mar 24 '10 at 11:39
 
Usually a EXC_BAD_ACCESS (commonly called segfault) which causes your program to crash. Those happen too if you have a reference to an object which is already deallocated and you try to access it. –  Fönsi Mar 24 '10 at 11:48

both imageWithData and dataWithContentsOfURL return autoreleased objects, so you should have no memory leaks in that code snippet.

Since flagimg is returned autoreleased, your [flagimg release]; call is not needed; you're over-releasing that object.

share | improve this answer
  

as a general rule you can say

if you create an object an theres a "init","copy" or "retain" in it, you have to release it.if not, you get an autoreleased object.

thats not always true, but in most cases


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值