开发者

Memory leak causing app to crash

开发者 https://www.devze.com 2023-02-14 16:37 出处:网络
My app gives a low memory crash on the device and not on the simulator.I used Instruments and i think that the problem lies in the following part

My app gives a low memory crash on the device and not on the simulator.I used Instruments and i think that the problem lies in the following part

       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

       [button setBackgroundImage:[UIImage imageNamed:@"gembtnblu.png"]  forState:UIControlStateNormal];<br>

        button.frame = CGRectMake(0, 0, TOOLBAR_BUTTON_WIDTH , TOOLBAR_BUTTON_HEIGHT);<br>

        [button setTitle:[NSString stringWithFormat:@"%c",choice] forState:UIControlStateNormal];<br>

        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        [button addTarget:self action:@selector(ChoiceButtonTouched:) forControlEvents:UIControlEventTouchUpInside];

        [button setTag:choice];

        UIBarButtonItem *c开发者_如何学PythonustomBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

        //Add button to the array
        [tempItems addObject:customBarItem];

        if (isReviewing == TRUE) {
            customBarItem.customView.userInteractionEnabled=FALSE;
        }
        //release buttons
        [customBarItem release];

        numberOfChoices++;

But im not able to spot the problem.Please help guuys, i have beeen stuck at this for days now

heres some more code

NSArray *items=[[NSArray alloc] initWithArray:(NSArray *)tempItems];

[tempItems release];

//add array of buttons to toolbar
[toolbar setItems:items animated:YES];
[self.view addSubview:toolbar];

the static analyser says that theres a potential leak with 'items' array.But if i put in a release statement, the app crashes


Did you release the toolbar after adding it as a subview? If you did not, and are not releasing the toolbar in the dealloc, there's your leak.


Is the image you're creating large? It could be that you're loading a huge message into memory and not releasing it.

I'd recommend running the static analyzer on this code (Build and Analyze), the errors should help you understand where you aren't releasing things that you should be.


The snippet of source code you posted does not contain memory management errors. I am assuming here that the image you load is small, since this is for a button. Note that imageNamed: caches the image but assuming the image is small this is not a problem.

Either the problem lies somewhere else in the code, or on your device you have too much applications open simultaneously. Beware of applications running in background: these still are eating memory. Try closing all of the applications and running again yours on the device. See if you experience exactly the same behavior when executing the same code snippet.


You may not be releasing old buttons and lists before allocating new ones. Perhaps you should reuse the old buttons instead of creating new similar ones.

0

精彩评论

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