线程1:EXC_BAD_ACCESS(代码= 13,地址= 0x0)

时间:2012-03-24 18:09:27

标签: objective-c xcode

有一个程序可以编译,但随后退出,返回上面的错误信息。

这是0 objc_msgSend日志;已注释掉错误消息的行:

libobjc.A.dylib`objc_msgSend:
0x7fff8c9b7e80:  testq  %rdi, %rdi
0x7fff8c9b7e83:  je     0x00007fff8c9b7eb0       ; objc_msgSend + 48
0x7fff8c9b7e85:  testb  $1, %dil
0x7fff8c9b7e89:  jne    0x00007fff8c9b7ec7       ; objc_msgSend + 71
0x7fff8c9b7e8c:  movq   (%rdi), %r11
0x7fff8c9b7e8f:  pushq  %rax
0x7fff8c9b7e90:  movq   16(%r11), %r10
0x7fff8c9b7e94:  movl   %esi, %eax
0x7fff8c9b7e96:  andl   (%r10), %eax           // error message arrow appears on this line
0x7fff8c9b7e99:  movq   16(%r10,%rax,8), %r11
0x7fff8c9b7e9e:  incl   %eax
0x7fff8c9b7ea0:  testq  %r11, %r11
0x7fff8c9b7ea3:  je     0x00007fff8c9b7edb       ; objc_msgSend + 91
0x7fff8c9b7ea5:  cmpq   (%r11), %rsi
0x7fff8c9b7ea8:  jne    0x00007fff8c9b7e96       ; objc_msgSend + 22
0x7fff8c9b7eaa:  popq   %rax

// Rest left out; no error messages

的main.m:

#import <Foundation/Foundation.h>
#import "Budget.h"

#import "Transaction.h"
#import "CashTransaction.h"
#import "CreditCardTransaction.h"

int main(int argc, const char * argv[])
{

    Budget *europeBudget = [Budget new];
    [europeBudget createBudget:1000.00 withExchangeRate:1.2500];

    Budget *englandBudget = [Budget new];
    [englandBudget createBudget:2000.00 withExchangeRate:1.5000];


    NSMutableArray *transactions = [[NSMutableArray alloc]initWithCapacity:10];
    Transaction *aTransaction;
    for (int n=1; n < 2; n++) {

        [aTransaction createTransaction:n * 100 forBudget:europeBudget];
        [transactions addObject:aTransaction];
        aTransaction = [CashTransaction new];
        [aTransaction createTransaction:n * 100 forBudget:englandBudget];
        [transactions addObject:aTransaction];
    }

    int n = 1;
    while (n < 4) {

        [aTransaction createTransaction:n * 100 forBudget:europeBudget];
        [transactions addObject:aTransaction];
        aTransaction = [CreditCardTransaction new];
        [aTransaction createTransaction:n * 100 forBudget:englandBudget];
        [transactions addObject:aTransaction];
        n++;
    }


        for (Transaction * aTransaction in transactions) {
            [aTransaction spend];
    }


            return 0;
}

Transaction.h

import

@class Budget;

@interface Transaction : NSObject
{
    Budget *budget;
    double amount;
}

- (void) createTransaction:(double)theAmount forBudget:(Budget*) aBudget;
- (void) spend;
- (void)trackSpending: (double) theAmount;

@end

Transaction.m

#import "Transaction.h"
#import "Budget.h"

@implementation Transaction

- (void) createTransaction:(double)theAmount forBudget:(Budget*) aBudget
{
    budget = aBudget;
    amount = theAmount;
}

- (void) spend
{
    // Fill in the method in subclasses
}

-(void)trackSpending: (double) theAmount
{
    NSLog(@"You are about to spend another %.2f", theAmount);
}
@end

2 个答案:

答案 0 :(得分:6)

我正在寻找解决SIGSEGV周围另一个问题的解决方案。

虽然回复有点迟,但为了记录和完整,我想回答这个问题。

出现问题的原因是您正在向对象发送消息而不实例化消息。

Transaction *aTransaction;
for (int n=1; n < 2; n++) {

    [aTransaction createTransaction:n * 100 forBudget:europeBudget];

和create方法定义为:

- (void) createTransaction:(double)theAmount forBudget:(Budget*) aBudget;

我会按如下方式编写这些方法。

首先,createTransaction对我来说听起来像工厂方法。所以,我会把它变成一个类方法,比如:

+(Transaction *) createTransactionWithAmount:(double) theAmount forBudget: (Budget *) aBudget;

然后我将使用如下的工厂方法:

for (int n=1; n < 2; n++) {
     Transaction *aTransaction = [Transaction createTransactionWithAmount: n*100 forBudget: europeBudget];
   // [aTransaction createTransaction:n * 100 forBudget:europeBudget];
    [transactions addObject:aTransaction];

这应解决代码中的2个问题:

1)您将始终有一个实例来发送消息,定义为aTransaction var
2)您不必担心将此对象添加到集合中,因为它不再是nil。

答案 1 :(得分:2)

aTransaction在声明时未分配值,因此它以垃圾值开始。第一次通过循环,你发送一条消息给那个垃圾值:崩溃!使用nil初始值声明变量将解决问题:

Transaction *aTransaction = nil;