2 Replies Latest reply: Sep 14, 2012 11:22 AM by MarionMck
MarionMck Level 1 Level 1 (0 points)

I'm trying to get some images shown responsively as the user slides a slider,  If I set up my image on each call of SliderValueChanging (the selector for control event UIControlEventValueChanged), as below, then I get an error indicating there's an event after I've released it.

 

    UIImage *btnimg = [UIImage imageNamed:imagefile]; 

    [button setImage:btnimg forState:UIControlStateNormal];

    [btnimg release];

 

The error occurs while the user is still using the slider and the message is:

 

[UIImage _isResizable]: message sent to deallocated instance

 

So I thought I'd store my images until the user has finished using the slider and release them then.  So I build them up in an array in SliderValueChanging as below:

 

    UIImage *btnimg = [UIImage imageNamed:imagefile]; 

    [button setImage:btnimg forState:UIControlStateNormal];

    // assign into our array for later release...

    if ( self.tmpimages == nil ) {

        //Initialize the array.

        self.tmpimages = [[NSMutableArray alloc] init];

    }

    [self.tmpimages addObject:btnimg];

    [btnimg release];

 

and then release them in SliderValueChanged (the selector for control event UIControlEventTouchUpInside)

 

    // tidy up our tmp images from sliding...

    if ( self.tmpimages != nil ) {

        [self.tmpimages removeAllObjects];

        [self.tmpimages release];

        self.tmpimages = nil;

    }

 

The sliding then works without any problems but I get an error when the sliding is finished indicating I'm trying to release an object which I've already released.

 

[UIImage release]: message sent to deallocated instance

 

The error is being generated from the releaseAllObjects call.

 

What am I doing wrong?  Can I prevent either of these error scenarios so that I get my images to show responsively with the slider without any problems?

 

Thanks!

  • Tod Kuykendall Level 4 Level 4 (2,270 points)

    Under the Cocoa naming conventions objects returned by convenience methods like:

     

        UIImage *btnimg = [UIImage imageNamed:imagefile];

     

    are passed you autoreleased. You should not send a release to this object and the errors you're getting reflect this. If you add it to an array this will add to the retain count but you should not release it after adding it to the array - just add it.

     

    The magic words in Cocoa are alloc, copy and new. If you use these words when you create it then you own it. If not assume something else owns it and will do the right thing. If you release something you don't own (or have explicitly retained) you're stepping on the process and you'll get errors like the ones you're getting.

     

    HTH,

    =Tod

  • MarionMck Level 1 Level 1 (0 points)

    Many thanks.  I was concerned that I wasn't being rigorous enough with the memory management, but it sounds like I've been over-active with it instead!