Search code examples
xcodeuiimageviewuiimageallocation

Why is my image leaking or being zombie?


I have an app that switches an image, but i get a lot of trouble... first "del" is de AppDelegate, and "displayImage" is defined in its .m "equiz" is an integer, but all the images are named "1", "2" and like that

So when i use this code i get leaks but no crash:

    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d",equiz]];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = del.displayImage.frame;
    del.displayImage = imgView;
    [imgView release];

Then i use this but i get a crash when i go to the "desktop" of the device, and return to the app:

    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d",equiz]];
    del.displayImage.image = img;
    [img release] //"that line"

Then you say: "you are releasing the object that you dont own" And i say: "If i delete "that line" i'll get the same leak D:

Can someone help me with this issue? Thank you very much in advance :D


Solution

  • Hi I was have similar problem and it was from NSString. So my solution was this: in .h file create

    NSString *imageName;
    @property(nonatomic, retain)NSString *imageName;
    

    then in .m file do this

    @synthesize imageName; ...

    imageName = [NSString stringWithFormat:@"%d",equiz];
    UIImage *img = [UIImage imageNamed:imageName];
        UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
        imgView.frame = del.displayImage.frame;
        del.displayImage = imgView;
        [imgView release];
    

    ...

    and in dealloc method release imageName...

    - (void)dealloc {
       [imageName release];
       [super dealloc];
    }
    

    By me this works .. Hope help is solution for you too.