Exemption of NSData throws an exception

Can someone explain why the following code makes my application bomb?

NSData *myImage = UIImagePNGRepresentation(imageView.image); : [myImage release]; 

If I comment out the "release" line, the application starts ... but calls the function containing this code several times and I get a crash - I think it was caused by a memory leak.

Even if I comment on EVERYTHING else in the function and just leave these two lines when the release is complete, the application crashes.

I am sure that this should be a beginner "you do not know how to properly clean your mess" ,-)

Greetings

Jamie

+4
source share
3 answers

Are you sure you should call release - the general rule of the MacOS API is that methods that transfer ownership have Copy or Create in their name. I suspect that you are given a link to the main representation of the image, not a copy, in which case you release an object belonging to something else.

+4
source

Look at memory management, you can find several threads here, or you can take a look at this page , I will not go into all the rules here, but the main problem is that myImage is auto-implemented and not saved - when you manually call release , it is not fraught with saving, so when an auto-advertisement tries to delete an object (now invalid) at the end of the execution cycle your application will crash. Removing the release will fix the problem, but take some time to familiarize yourself with the save / release rules, this is one of the most important things to understand.

+4
source

You must not let go of this object! Apple says: "Return value An autodetected data object containing PNG data, or nil if there is a problem creating the data." (See Link to UIImagePNGRview )

0
source

All Articles