Objective c 使用NSMutableArray时内存泄漏

Objective c 使用NSMutableArray时内存泄漏,objective-c,memory-leaks,nsmutablearray,release,Objective C,Memory Leaks,Nsmutablearray,Release,大家好,有人能告诉我如何修复下面代码中的内存泄漏吗 我已经尝试了我能想到的每一种发布和自动释放的组合,但每次应用程序崩溃或泄漏仍然存在 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ //get refereance to the textfield UITextField *currentTextField = (UIText

大家好,有人能告诉我如何修复下面代码中的内存泄漏吗

我已经尝试了我能想到的每一种发布和自动释放的组合,但每次应用程序崩溃或泄漏仍然存在

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

//get refereance to the textfield
UITextField *currentTextField = (UITextField*)[self.view viewWithTag:200];

//check which picker
if(pickerView.tag ==1)
{   
    // Only calls the following code if component "0" has changed.
    if (component == 0) {

        // Sets the global integer "component0Row" to the currently selected row of component "0"
        component0Row  = row;

        // Loads the new values for the selector into a new array in order to reload the data.
    newValues = [[NSMutableArray alloc] initWithArray:[pickerData objectForKey:[selectorKeys objectAtIndex:component0Row]]];
        currentValues = newValues;


        // Reloads the data of component "1".
        [pickerView reloadComponent:1];


    }

    //run the selector logic
    [self textFieldDidEndEditing:currentTextField];

}
希望有人能提出建议


非常感谢

您的NSMutableArray分配从未发布过

newValues = [[NSMutableArray alloc] initWithArray:[pickerData objectForKey:[selectorKeys objectAtIndex:component0Row]]]; newValues=[[NSMutableArray alloc]initWithArray:[pickerData objectForKey:[selectorKeys objectAtIndex:component0Row]];
您应该自动释放它,或者在以后知道不再需要它时释放它。

不确定如何定义CurrentValue,但这应该可以在没有泄漏的情况下工作:

在.h文件中:

@property (nonatomic, retain) NSArray * currentValues;
在.m文件中:

@synthesize currentValues;

self.currentValues = newValues;

您的问题在于以下两行:

newValues = [[NSMutableArray alloc] initWithArray:[pickerData objectForKey:[selectorKeys objectAtIndex:component0Row]]];
currentValues = newValues;
第一行分配了NSMutableArray的一个新实例。第二行将指针从
newValues
复制到
currentValues
,覆盖
currentValues
中的指针值。无论
currentValues
指向什么,都将丢失。这就是漏洞

您可以这样修复它:

newValues = [[NSMutableArray alloc] init...
[currentValues release];
currentValues = newValues;
通过这种方式,
currentValues
指向的任何对象的引用计数都会在您无法访问它之前递减


您还可以通过将CurrentValue设置为Objective-C属性,并通过
self.currentValues
[self-setCurrentValues:
使用访问器方法来解决此问题;这些方法将为您处理保留/释放。

Hi Alex我已尝试自动释放,但我在dealloc中发布的应用程序崩溃,但这并不能阻止仪器在viewdidload中分配Hi perception currentValues后发现泄漏,如下currentValues=[[NSMutableArray alloc]initWithArray:[pickerData objectForKey:[SelectWorkeys objectAtIndex:component0Row]];@superllanboy-currentValues是您视图的属性还是您在函数中声明的变量?如果是后者,则您发现了泄漏。hi perception currentValues是属性Yok,则您的泄漏最有可能发生,因为您将当前值用作ivar(无论何时替换其值,都会泄漏原来所在的旧对象)。与其做
currentValue=someobj
,不如做
self.currentValue=someobj
。您好Benzado,谢谢您的建议,但我之前已经厌倦了这个版本,刚才又一次,当我滚动选取器时,应用程序崩溃,exc访问不正确。当您询问泄漏问题时,它会让我发疯。这些答案将堵塞你的漏洞。崩溃是另一个问题。谷歌搜索说明并在
objc_exception_throw
上设置断点,这样你就可以找出错误的指针是什么。如果你不能弄清楚,请问一个新问题。如果我采用你建议的代码来堵塞漏洞,就会发生崩溃。堵塞的漏洞是否可能会在代码的其他部分?我不太明白,所以希望你能详细解释一下,谢谢