2012-03-24 72 views
0

有一个程序编译,但然后退出,返回上述错误信息。线程1:EXC_BAD_ACCESS(代码= 13,地址= 0x0)

这里是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

进口

@class预算;

@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 
+0

纠正我,如果我错了,但0x0是NULL为十六进制。我想你是在传递一个空对象。 – CodaFi 2012-03-24 18:13:44

+0

认为它可能位于NSMutableArray对象中,但找不到它。已经添加了main.m文件供您阅读。 – pdenlinger 2012-03-24 18:26:40

+2

发送消息给一个无对象是完全没问题的。它返回零,编译器将其转换为函数的返回类型。 (这是一个问题,如果你的返回类型是NSRect,但是新版本的SDK可以正确处理,正如我所记得的那样。)objc_messageSend中的崩溃通常意味着你正在向释放的对象发送消息。 – davehayden 2012-03-24 18:29:07

回答

2

aTransaction未分配时,它的声明价值,所以它有一个垃圾值开出。通过循环第一次,你发送一条消息给那个垃圾值:崩溃!具有零初始值声明的变量将解决这个问题:

Transaction *aTransaction = nil; 
+0

问题:Transaction类有两个实例变量:预算和金额。如果默认的ivars在枚举函数中被填充,它为什么会崩溃?我已经添加了Transaction.h,.m文件供您查看。将代码更改为“Transaction * aTransaction = nil;”但我碰到了。在[transactions addObject:aTransaction]上获得了线程1:SIGABRT信号;行 – pdenlinger 2012-03-24 19:17:12

+0

我不确定你的意思是“枚举函数”。如果您正在初始化aTransaction,我在代码中看不到任何明显的错误。尝试开启NSZombieEnabled(谷歌的方向),看看它说什么。 – davehayden 2012-03-25 00:16:06

+0

抱歉,延迟响应;我只是试图从头开始重新编码,看看我是否得到了同样的错误,而且我做到了。对于枚举函数,我的意思是循环。通过启用NSZombie Enabled将会看到我能找到的东西。 – pdenlinger 2012-03-26 19:41:47

6

我在这个问题上降落,而我一直在寻找一个解决方案,围绕SIGSEGV另一个问题。

虽然有点迟,但为了文件和完整性,我想回答这个问题。

问题的产生是因为您发送消息给对象而没有实例化一个对象。

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

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

和创建方法被定义为:

- (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)您不必担心将此对象添加到集合中,因为它不会再是零。

相关问题