Question

i want to write my own photogallery like the original "Photos.app" from apple. I´ve created a UITabbarcontroller in the AppDelegate and then an "ImageViewController" and a "VideoViewController".

In the "ImageViewController" i´ve added an UIScrollView and then made an instance of my own "PhotoGallery" with different properties like imagePerRow, images, paddings etc.

For the "PhotoGallery" i´ve created a new objective-c class as a subclass of "NSObject", where i´m positioning all the different images as UIButtons. Then i´ve added another function which describes the arrangement for all the images when the device orientation has changed. And the dealloc-function. Thats all.

This class works great, also the rearrangement when the device orientation has changed. The problem is, if i simulate a memory warning in the ios-simulator, the first time the PhotoGallery gets correctly dealloc but if i simulate a warning again, i get a error-message: "[PhotoGallery release]: message sent to deallocated instance ".

I thought its because of the subclass as NSObject, right? Then i´ve tested it as a UIView. With the same error. So know i don´t know what to do anymore. Hope you understand what´s the problem and you would give me some hints on that.. Think about calling the init-function again? How? Need "drawRect"? I´ve no idea.

Thanks for your time and help, G.

Was it helpful?

Solution

You're probably not setting the property which holds a reference to the PhotoGallery to nil.

ie. You're keeping a reference to a deallocated instance, and attempting to call release on it.

bad example:

- (void) didReceiveMemoryWarning
{
    [photoGallery release];
}

safe(r) example:

- (void) didReceiveMemoryWarning
{
    [photoGallery release];
    photoGallery = nil;

    // or combine both actions if your property attributes are set up to accommodate it:
    // self.photoGallery = nil;
}

In the bad example, photoGallery still holds a reference to a now-deallocated instance, and the second memory warning will attempt to send a message to it.

In the safe(r) example, photoGallery is nil, and sending a message to nil is safe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top