开发者

Memory Leak Warnings, Due To NSArray Of Large Images

开发者 https://www.devze.com 2023-03-12 14:06 出处:网络
I have an iPad app, which is children\'s book. You navigate from one page to the next by calling this action, which essentialy fades out the current page and fades in the next page, then fills the thr

I have an iPad app, which is children's book. You navigate from one page to the next by calling this action, which essentialy fades out the current page and fades in the next page, then fills the three stack UIImageViews with the next row of the NSArray, so it's prepared to complete the action for the next page:

-(void)delayedMethod{

    [leftButton setAlpha:1];

    [pageImageNext setAlpha:0];
    [pageImage setAlpha:1];
    [pageImageBack setAlpha:1];

    NSString *imageExtension = @".jpg";
    NSString *audioExtension = @".wav";

    if (activePage == thePages.count/3)
    {
        a开发者_开发知识库ctivePage = 1;
    }
    else 
    {
        activePage = activePage + 1;
    }

    NSInteger row = 0;
    if(activePage == 0)
    {
        row = activePage;
    }
    else
    {
        row = ((activePage) * 3);
    }   

    [leftButton setAlpha:1];

    pageImageBack.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", [thePages objectAtIndex:row+0], imageExtension]];
    pageImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", [thePages objectAtIndex:row+1], imageExtension]];
    pageImageNext.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", [thePages objectAtIndex:row+2], imageExtension]];

    [UIImageView beginAnimations:NULL context:NULL];
    [UIImageView setAnimationDuration:.5]; // you can set this to whatever you like
    /* put animations to be executed here, for example: */

    [leftButton setAlpha:0];
    [rightButton setAlpha:0];
    [leftButton setAlpha:1];
    [rightButton setAlpha:1];
    leftButton.hidden = NO;


    /* end animations to be executed */
    [UIImageView commitAnimations]; // execute the animations listed above

    [imageExtension release];


}

The NSArray is loaded in this action, which is called in the ViewDidLoad:

-(void)loadPages
{
    thePages = [[NSArray alloc] initWithObjects:
                   //   How many stacked images do we need to get fade in and outs? Also add column fo
                @"page0",@"page0",@"page1",
                @"page0",@"page1",@"page2",
                @"page1",@"page2",@"page3",
                @"page2",@"page3",@"page4",
                @"page3",@"page4",@"page5",
                @"page4",@"page5",@"page6",
                @"page5",@"page6",@"page7",
                @"page6",@"page7",@"page8",
                @"page7",@"page8",@"page9",
                @"page8",@"page9",@"page10",
                @"page9",@"page10",@"page11",
                @"page10",@"page11",@"page12",
                @"page11",@"page12",@"page13",
                @"page12",@"page13",@"page14",
                @"page13",@"page14",@"page15",
                @"page14",@"page15",@"page16",
                @"page15",@"page16",@"page17",
                @"page16",@"page17",@"page18",
                @"page17",@"page18",@"page19",
                @"page18",@"page19",@"page20",
                @"page19",@"page20",@"page21",
                @"page20",@"page21",@"page22",
                @"page21",@"page22",@"page23",
                @"page22",@"page23",@"page24",
                @"page23",@"page24",@"page25",
                @"page24",@"page25",@"page26",
                @"page25",@"page26",@"page27",
                @"page26",@"page27",@"page27",
                nil];

}

It all works just fine until about the 10-12th page when I start to get memory warnings in the console and usually a crash.

I am pretty sure it is just a matter of releasing the three large UIImageViews at the right time, but I can't figure out when...I've tried a number of different spots in the code.


I was doing an app like this, where I had a lot of image views and labels that the user could change. I got warnings and crashes all the time. The only thing that seemed to fix it all was to make them all properties. Everything I wanted to hang on to on a certain view I had to make a property. That took care of it nicely, otherwise it seemed like the OS freaked out that I had to much allocated, and would send memory warnings which would release stuff, and then when the app tried to access them it would crash. See if making your pageImageNext, pageImage , pageImageBack properties works.


Make sure you are doing a [thePages release]]; in the dealloc method of the view controller. Not releasing that will definitely cause memory issues.

Also have you made the imageviews retained properties in the view controller? If so, you need to release them in the dealloc as well.

As for memory warnings, you can unset the imageviews in didRecieveMemoryWarning and then just add in a check to re-create them as needed.


The code:

[imageExtension release];

Is incorrect and should be deleted. "imageExtension" is allocated on the stack, and will go away when the method exits. Only release things that you have alloc'd.

Do you have just one of these types of ViewControllers? Or one for each page?


@sasquatch @YuzaKen @MishieMoo If I simply set self.pageImage = nil; in the dealloc, as you suggested, won't that only release the memory when a user leaves the ViewController?

I feel like I need to be releasing the UIImageViews as I move down the rows of the array, when I replace the contents of the UIImageView with a new image, no?

0

精彩评论

暂无评论...
验证码 换一张
取 消