xcode 线程 1:EXC_BAD_ACCESS(代码 = 13,地址 = 0x0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9854183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread 1: EXC_BAD_ACCESS (code=13, address = 0x0)
提问by pdenlinger
Have a program which compiles, but then quits, returning the above error message.
有一个程序可以编译,但随后退出,返回上述错误消息。
Here is the 0 objc_msgSend log; have commented out the line the error message appears on:
这是 0 objc_msgSend 日志;已注释掉错误消息出现的行:
libobjc.A.dylib`objc_msgSend:
0x7fff8c9b7e80: testq %rdi, %rdi
0x7fff8c9b7e83: je 0x00007fff8c9b7eb0 ; objc_msgSend + 48
0x7fff8c9b7e85: testb , %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:
主.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
事务.h
import
进口
@class Budget;
@class 预算;
@interface Transaction : NSObject
{
Budget *budget;
double amount;
}
- (void) createTransaction:(double)theAmount forBudget:(Budget*) aBudget;
- (void) spend;
- (void)trackSpending: (double) theAmount;
@end
Transaction.m
交易.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
回答by alp
I landed on this issue while I was looking for a solution to another problem around SIGSEGV.
我在寻找解决 SIGSEGV 周围另一个问题的方法时遇到了这个问题。
Although it is a bit late response, I would like to answer this question for the sake of documentation and completeness.
虽然回复有点晚,但为了文档和完整性,我想回答这个问题。
The problems arises because you are sending messages to an object without instantiating one.
出现问题是因为您将消息发送到一个对象而没有实例化一个对象。
Transaction *aTransaction;
for (int n=1; n < 2; n++) {
[aTransaction createTransaction:n * 100 forBudget:europeBudget];
and create method was defined as:
和 create 方法定义为:
- (void) createTransaction:(double)theAmount forBudget:(Budget*) aBudget;
I would write these methods as follows.
我会写这些方法如下。
First of all, createTransaction sounds like a factory method to me. So, I would make it a class method, like:
首先,createTransaction 对我来说听起来像是一个工厂方法。所以,我会把它变成一个类方法,比如:
+(Transaction *) createTransactionWithAmount:(double) theAmount forBudget: (Budget *) aBudget;
Then I would use that factory method like below:
然后我将使用如下工厂方法:
for (int n=1; n < 2; n++) {
Transaction *aTransaction = [Transaction createTransactionWithAmount: n*100 forBudget: europeBudget];
// [aTransaction createTransaction:n * 100 forBudget:europeBudget];
[transactions addObject:aTransaction];
That should fix 2 problems with the code:
这应该解决代码的两个问题:
1) You will always have an instance to send messages against defined as aTransaction var
2) You don't have to worry about adding this object into the collection as it won't be nil anymore.
1)您将始终有一个实例来发送消息,定义为 aTransaction var
2)您不必担心将此对象添加到集合中,因为它不再为零。
回答by davehayden
aTransaction isn't assigned a value when it's declared, so it starts out with a garbage value. The first time through the loop, you're sending a message to that garbage value: crash! Declaring the variable with a nil initial value will fix the problem:
aTransaction 在声明时未分配值,因此它以垃圾值开始。第一次循环时,您将向该垃圾值发送一条消息:崩溃!使用 nil 初始值声明变量将解决问题:
Transaction *aTransaction = nil;