内存泄漏导致应用崩溃

时间:2011-03-04 14:31:29

标签: iphone xcode memory-management crash

我的应用程序在设备上造成内存崩溃,而不是在模拟器上。我使用了仪器,我认为问题出在以下部分

       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 *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

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

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

        numberOfChoices++;

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 *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; //Add button to the array [tempItems addObject:customBarItem]; if (isReviewing == TRUE) { customBarItem.customView.userInteractionEnabled=FALSE; } //release buttons [customBarItem release]; numberOfChoices++;

但是我无法发现问题。请帮助guuys,我已经被困在这几天了

继承了更多代码

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

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

静态分析仪表示可能存在“物品泄漏”的情况。 array.But如果我放入一个发布语句,应用程序崩溃

4 个答案:

答案 0 :(得分:0)

您创建的图片是否很大?可能是你正在向内存中加载一条巨大的信息而不是释放它。

我建议在此代码(Build and Analyze)上运行静态分析器,这些错误应该可以帮助您了解您不应该发布的内容。

答案 1 :(得分:0)

您发布的源代码片段不包含内存管理错误。我假设您加载的图像很小,因为这是一个按钮。请注意,imageNamed:缓存图像,但假设图像很小,这不是问题。

问题出在代码中的其他位置,或者在设备上有太多应用程序同时打开。注意在后台运行的应用程序:这些仍然在吃内存。尝试关闭所有应用程序并再次在设备上运行。查看执行相同代码段时是否遇到完全相同的行为。

答案 2 :(得分:0)

在分配新按钮和列表之前,您可能无法释放旧按钮和列表。也许您应该重复使用旧按钮,而不是创建新的类似按钮。

答案 3 :(得分:0)

在将工具栏添加为子视图后,您是否已将其发布?如果你没有,并且没有在dealloc中释放工具栏,那就是你的泄密。

相关问题