简体   繁体   English

我的程序中出现错误“ EXC_BAD_ACCESS”

[英]error “EXC_BAD_ACCESS” in my program

i have a problem. 我有个问题。 When i compile my program there isn't a error but, when i start it these retern with “EXC_BAD_ACCESS”. 当我编译程序时,没有错误,但是,当我启动程序时,这些提示都带有“ EXC_BAD_ACCESS”。 I looking for the error with the debug and i find it in these method but i don't understand where... 我正在寻找调试错误,并且在这些方法中找到了错误,但我不知道在哪里...

PS:the program enters in the loop sometimes. PS:程序有时会进入循环。

-(void)updateMinPosition{

    float valueMinX = 150;
    float valueMinY = 150;
    float valueMinZ = 150;

    NSString *nameMinimoX = [NSString   stringWithFormat:@"default"];
    NSString *nameMinimoY = [NSString   stringWithFormat:@"default"];
    NSString *nameMinimoZ = [NSString   stringWithFormat:@"default"];

    for(int i = 0; i< [arrayPosizioniMovimento count]; i++){

        //Posizione is my class. It contain a NSString name, 3 float valueX, valueY, valueZ
        Posizione *tempPosition;
        tempPosition = [[Posizione alloc]init];

        tempPosition = [arrayPosizioniMovimento objectAtIndex:i];

        if(tempPosition.valueX <= valueMinX){
            valueMinX = tempPosition.valueX;
            nameMinimoX = tempPosition.nome;
        }

        if(tempPosition.valueY <= valueMinY){
            valueMinY = tempPosition.valueY;
            nameMinimoY = tempPosition.nome;
        }

        if(tempPosition.valueZ <= valueMinZ){
            valueMinZ = tempPosition.valueZ;
            nameMinimoZ = tempPosition.nome;
        }

        [tempPosition dealloc];
    }

    labelMinX.text = nameMinimoX;
    labelMinY.text = nameMinimoY;
    labelMinZ.text = nameMinimoZ;   
}

It looks like it may be the [tempPosition dealloc] call. 看起来可能是[tempPosition dealloc]调用。 You're declaring that variable and doing an alloc/init on it but then just assigning it to the object within the array, so the alloc/init is unneeded. 您在声明该变量并对其执行alloc / init,但随后将其分配给数组内的对象,因此不需要alloc / init。 When you make the call to dealloc at the bottom you're releasing that object that resides in the array, so your array will now have a null value which will cause the EXEC.... error 当您在底部调用dealloc时,将释放驻留在数组中的对象,因此您的数组现在将具有null值,这将导致EXEC ....错误

For the 1st glance there're several problems with your code: 乍一看,您的代码有几个问题:

    Posizione *tempPosition;
    tempPosition = [[Posizione alloc]init];

    tempPosition = [arrayPosizioniMovimento objectAtIndex:i];

Here in the second line you create new Posizione instance and right after that assign another value to the same variable. 在第二行中,您将创建新的Posizione实例,并在此之后立即将另一个值分配给同一变量。 In effect that will mean that your created instance will be never used and cause memory leak. 实际上,这将意味着将永远不会使用您创建的实例并导致内存泄漏。 To use element from array just write 要使用数组中的元素,只需编写

Posizione *tempPosition = [arrayPosizioniMovimento objectAtIndex:i];

The second one - is the following line 第二个-是以下行

[tempPosition dealloc];

First of all you should never call this method directly but rather send an object -release message - it will be deallocated automatically when its retain count becomes 0. In your case you do not retain tempPosition object in that code so there;s no need to release it here - just remove that line. 首先,您永远不要直接调用此方法,而应该发送对象-release消息-当其保留计数变为0时,它将自动释放。在这种情况下,您无需在该代码中保留tempPosition对象,因此无需在这里释放它-删除该行。

PS Using fast enumeration can also make your code more readable and less error prone: PS使用快速枚举还可以使您的代码更具可读性,并且不易出错:

for (Posizione *tempPosition in arrayPosizioniMovimento){
   if(tempPosition.valueX <= valueMinX){
...

You can enable Objective-C Exception breakpoints to pinpoint the line. 您可以启用Objective-C异常断点来精确定位该行。

On thing that jumps out is 跳出来的是

    [tempPosition dealloc];

I'm not sure what to tell you to do. 我不确定该如何告诉您。 You need to call release not dealloc. 您需要调用release而不是dealloc。

[tempPosition release];

But im not sure how that fits into the rest of your code, where you are allocating a variable then immediately assigning it another value. 但是我不确定它是否适合您的其余代码,您将在其中分配变量,然后立即为其分配另一个值。

I think you should just remove the alloc and dealloc 我认为您应该删除alloc和dealloc

so delete: 所以删除:

    //Posizione is my class. It contain a NSString name, 3 float valueX, valueY, valueZ
    Posizione *tempPosition;
    tempPosition = [[Posizione alloc]init];

and

[tempPosition dealloc];

Pease show how do you initialize arrayPosizioniMovimento . arrayPosizioniMovimento如何初始化arrayPosizioniMovimento If you initialized it like arrayPosizioniMovimento = [NSMutableArray arrayWithCapacity:n]; 如果您像arrayPosizioniMovimento = [NSMutableArray arrayWithCapacity:n];一样初始化它arrayPosizioniMovimento = [NSMutableArray arrayWithCapacity:n]; than you'll need to add [arrayPosizioniMovimento retain]; 您将不需要添加[arrayPosizioniMovimento retain];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM