0

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.

geforce
  • 2,593
  • 3
  • 28
  • 44
  • 1
    This doesn't answer your question (hence, adding it as a comment) but have you considered using three20? They have a great implementation of most of the Photos app stuff. – donkim Jan 05 '11 at 23:35
  • Yeah. Thanks for reply. I want to learn how to write that stuff on my own. I had a look at the three20 source but its too difficult for me at the moment, so i decide to learn it step by step.. – geforce Jan 06 '11 at 12:03

1 Answers1

1

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.

codelark
  • 12,254
  • 1
  • 45
  • 49
  • YEAHHHH! Great! It´s so easy to fix?! Nice! I thought that my ViewController or Subclass of NSObject has to be fixed. But exactly, i didn´t set the photoGallery to nil! – geforce Jan 06 '11 at 12:12
  • But one question again: Does the PhotoGallery class has to be a sublcass of NSObject or UIView? Whats the advantages for both? – geforce Jan 06 '11 at 12:14
  • the memory management methods are declared on NSObject. Subclassing from UIView gets you all the built-in functionality of a View. When a question comes up about inheritance ask "Is my class a ?" If so, then inherit from it. – codelark Jan 06 '11 at 14:24