user674224
user674224

Reputation: 11

iPhone app memory issues. Crashing due to Low Memory?

My iPhone app is crashing like crazy. I need some help, or really direction.

Quick background: I don't know how to program that well. I've only been doing it for a few months and I'm pretty sure I didn't make this app in the best possible way, or even close.

The app is basically a menu screen with 10 buttons. Each button leads to 3 pages and the navigation will take you back and forth, and back to the main menu. It only has one view and all of the images load as you start the app. All of the images are hidden at first, and as needed they will be shown (hidden = NO).

The app loads up very slowly and then functions exactly as needed. After clicking through about 15 of the images, it will crash.

I've used instruments (not fully sure how to yet) but it doesn't show any memory leaks. It shows "low memory" a bunch of times before crashing.

So time for my questions. I apologize that these are very amateur. This is the first time I've seen any crashes in my apps, and this one seemed so simple to me. It has a lot more imagery than anything I've done before.

  1. Without starting the app over from scratch, is there anything I can do to fix the issue and eliminate the crashes? I don't fully understand how to handle memory (obviously). I know that anytime you alloc something you should release it, but I don't know that I ever do alloc it. All I'm doing is showing and hiding images.

  2. If I have to start over from scratch, what is the best way to do an app like this (similar to a book. main window for navigation, and 10 buttons that lead to 3 full screen images each)? I've never worked in more views than the utility template.

  3. What other information can I provide you to help me?

I appreciate this! I created this app for a friend and I've been struggling for a few days trying to make it right.

EDIT:

(add your analyze details and code here). Put 4 spaces in front of your code and ensure formatting.

- (IBAction)pressNext:(id)sender
{
if (num == 11)
{
    peteNotes1.hidden = YES;
     //[peteNotes1 release], peteNotes1 = nil;
    peteBio.hidden = NO;
    num = 12;
    //[self playRandomSound];
}
else if (num == 12)
{
    peteBio.hidden = YES;
     //[peteBio release], peteBio = nil;
    petePic.hidden = NO;
    next.hidden = YES;
    num = 13;
    //[self playRandomSound];
}
else if (num == 21)
{
    eddieNotes1.hidden = YES;
     //[eddieNotes1 release], eddieNotes1 = nil;
    eddieBio.hidden = NO;
    num = 22;
    //[self playRandomSound];
}
else if (num == 22)
{
    eddieBio.hidden = YES;
     //[eddieBio release], eddieBio = nil;
    eddiePic.hidden = NO;
    next.hidden = YES;
    num = 23;
    //[self playRandomSound];
}

Upvotes: 1

Views: 2005

Answers (1)

bryanmac
bryanmac

Reputation: 39296

Please add some code and I'll add more details.

Two things you can do right now:

  1. Run build with analyze. It will show you where to focus.
  2. Read and re-read the apple memory guide.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

Specifically, the four rules on the second page.

Don't start over - stick with it and understand what's going on. That's the best way to learn. It's hard at first but you'll get it soon.

[2 cents]Other advice - and this is my personal opinion. Learn how to code the apps from scratch and then use interface builder as a convenience (not a crutch). I like the Zdziarski early iPhone SDK book for that reason. I realize others may not agree (and that's fine) but starting with code really helps you understand what's going on better.[/2 cents]

EDIT:

After looking at the code, I realized fixing this is beyond the scope of the SO question. But, I do have a few pieces of advice where to focus.

  1. Spend some more time going through books and tutorials. It's obvious from the snippet above, you just need to work through books and structured guidance before going out on your own app.
  2. Look into arrays. The if else with nums and setting setting the next num is a really bad coding pattern. Click next should increment a member variable property and your data should be in arrays. If you're doing large switch blocks or large if else blocks, it's a sign you're doing something wrong.
  3. Rethink the pattern. Find a way to load the images on demand. And, figure out how to unload/release the images when not needed. You have a slow startup and memory foot print by loading up front and just hiding.
  4. Look at the size of the images. Have you resized them down to be the optimal size and pixels per inch on an iphone?

Hopefully the initial pointers and these help you going a little closer to the right direction.

Upvotes: 1

Related Questions