Skip navigation

problem showing images from UISlider

389 Views 2 Replies Latest reply: Sep 14, 2012 11:22 AM by MarionMck RSS
MarionMck Calculating status...
Currently Being Moderated
Sep 14, 2012 9:03 AM

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?



  • Tod Kuykendall Level 4 Level 4 (2,270 points)
    Currently Being Moderated
    Sep 14, 2012 10:11 AM (in response to MarionMck)

    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.





More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.