[Solved] How to release memory in objective-c?


For screenshot #2 it looks like you’re not releasing the CGImageRef object. For this, to clean this up you should use:

CGImageRelease(image); 

…when you’re done using the CGImageRef.

More info on this (and how it differs from CFRelease) can be found at this question: Releasing CGImage (CGImageRef) Mind you, even if you’re using ARC you need to be careful when using any of the C-based APIs, especially if you’re mixing them together with some Obj-C objects.

For the first screenshot and the code you posted it’s difficult to say since the logic is quite complex, however I’d suggest if you’re using ARC then I’d question if using ‘autorelease’ on all of your initializers is really necessary. When I try to use ‘autorelease’ in an ARC project it won’t even let me: Xcode gives the message “ARC forbids explicit message send of ‘autorelease’.” You may want to confirm that you really do have ARC turned on for this project.

In case it is helpful, this question discusses turning on ARC for your project: How to enable/disable ARC in an xcode project?


EDIT for newly added screenshot

The error message from Xcode for this screenshot pretty clearly indicates where the problem is here. When calling ‘CGColorSpaceCreateDeviceRGB’, this creates an object that you are responsible for releasing explicitly.

If you check the documentation on CGColorSpaceCreateDeviceRGB you’ll see that this is also stated in the documentation ‘Returns’ description:

CGColorSpaceCreateDeviceRGB() Returns Description

Therefore you’ll want to call ‘CGColorSpaceCreateDeviceRGB’ before you create the CGContextRef and you’ll need to release it after you’re done with the context using CGColorSpaceRelease as such:

CGColorSpaceRef myColorSpaceRef = CGColorSpaceCreateDeviceRGB();

CGContextRef myContext = CGBitmapContextCreate(...);

...

CGContextRelease(myContext);

CGColorSpaceRelease(myColorSpaceRef);

6

solved How to release memory in objective-c?