Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

problem showing images from UISlider

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];

[buttonsetImage: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];

[buttonsetImage: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.tmpimagesremoveAllObjects];

[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!

Posted on Sep 14, 2012 9:03 AM

Reply
Question marked as Best reply

Posted on Sep 14, 2012 10:11 AM

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

2 replies
Question marked as Best reply

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.


HTH,

=Tod

problem showing images from UISlider

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.