Jim
Jim

Reputation: 5960

release message sent to deallocated instance, but the instance pointer was not nil

Here is the code within one of my methods, where imageRaw is an ivar:

if (imageRaw) 
    [imageRaw release]; // error occurs here
    .
    .
    .
if (anImage) 
    imageRaw = [[anImage scaleToFitWithin:maxImageSize interpolationQuality:kCGInterpolationHigh] retain];          

I have had this type of error occasionally, and I don't really understand it. The only way it is non-nil is by assignment to an instance of the right class, UIImage, in this case.

Zombies are enabled.

All I am trying to do in a case like this is cause the existing instance to be deallocated so I can instantiate and assign/retain a new to the same pointer.

Upvotes: 0

Views: 115

Answers (1)

Sean
Sean

Reputation: 5820

I'm guessing what's happening here is that at some point in your execution, this method is being called when you have something stored in imageRaw but don't have anything in anImage. In this case, your object in imageRaw will be released but nothing new will be stored to it. This leaves the memory address for the deallocated object in imageRaw but the object no longer exists. Then, the next time you run the method, it checks to see if imageRaw is non-nil (which it is, since it still holds that memory address), tries to call release on that memory, and fails. To avoid this, you can make sure to set imageRaw to nil every time you call release on it:

if (imageRaw)
    [imageRaw release];
    imageRaw = nil;
    .
    .
    .
if (anImage)
    imageRaw = . . .

Upvotes: 1

Related Questions