views:

60

answers:

4

Hi, i release an image with [myimageview.image release];

but after the app was in background it comes foreground again it release that image again, so it crash! All my tries to check if the app came from a background did not worked. So how could i check if an object is already released.. so i dont do it twice?

thankx chris

EDIT After several complains about my bad coding :) here an example:

First I initialize an Array with the path to a lot of fullscreen images Also there are around 10 Different Arrays for 10 different Scenes. When I put directly the Images in an array it just needed to much memory from the beginning, or when i released a scene totaly , it needed to load the whole image array again and that came to slow. So I just load the path to the images into an array and assign each pic in a loop to my imageview while runtime.

Init once: imageArray_stand= [[NSArray alloc] initWithObjects: @"FrankieArmeRaus_0001.jpg", ... up to 60 Images @"FrankieArmeRaus_0061.jpg",nil];

In a Loop thats called each 1/10 Second:

      if ([myimageview.image  retainCount] > 1) 
        //if ( myimageview.image != nil) // does not work = crash
        {   
             [myimageview.image release]; 
              myimageview.image = nil;
        }

        myimageview.image = [UIImage imageNamed:[imageArray_stand2 objectAtIndex:piccounter-1]];

Problem came, because when the app went into background and than into foreground again it seems to release the image 2 times, so i needed a solution to check that.

I am happy about any solution (just NOW it works) thats better. Even to load all images completly into an array, but as mentioned it needs to much mem and to reassign while runtime needs to long to load. (1 sec for 50 Images)

Also I needed to RELASE and set to NIL, because otherwise it would even make my memory usage out of limit.

+1  A: 

Easiest way is to do something like this:

if (someObject != nil)
{
    [someObject release];
    someObject = nil;
}

However, I don't think you should be releasing myimageview.image, assuming that myimageview is a UIImageView. The UIImageView is responsible for managing its image; you shouldn't be messing with its retain count.

Maybe what you really want to do is this:

myimageview.image = nil;

This will cause myimageview to release it and stop pointing to the now-invalid memory.

Kristopher Johnson
did not worked.. solved it now with: if ([myimageview.image retainCount] > 1) [myimageview.image release];
christian Muller
Using `retainCount` is a bad idea. It should only be used for debugging, not for actual program logic, because it does not necessarily represent the actual number of retaining objects. What you are doing may seem to work, but it's not guaranteed to work.
Kristopher Johnson
+2  A: 

You never, ever, release a property of another object. You release the entire myimageview object, or you just assign to it's properties. How those properties are memory manged is the private business of the myimageview object.

calmh
in that case i had to !... also its solved now..thx
christian Muller
@calmh is right. There should never be a situation where you release other object's properties for it. I doubt that you have really "solved" it; you have just hacked around a bug.
Kristopher Johnson
A: 

solved it with

if ([myimageview.image retainCount] > 1) [myimageview.image release];

christian Muller
A: 

The following is NOT a solution:

if ([myimageview.image retainCount] > 1) [myimageview.image release];

This is really bad. Really bad. Really, really truly bad. I cannot do full justice to the sense of terror that I feel at seeing this.

It shows that you don't have a grasp of the basics of how the Obj-C memory management system works or the conventions that you should follow. Please stop what you're doing and spend a little time doing some reading on this topic.

I'm worried that some other poor sap might come here, see this and think (mistakenly) that it's a great idea.

No one in particular
I love critic, even you make it very dramatic.. what you do is not constructiv... so GIVE A Example when you are already so wise. Yes, I am a beginner... also i did read as good I understood the Memory management... but did not found another solution. Thats all.
christian Muller
you may read me EDIT.. in the question. thx
christian Muller