消息导致EXC_BAD_ACCESS(使用新信息更新)

时间:2011-04-30 13:19:21

标签: objective-c

这是我的代码,我收到上述错误;导致错误的行是对“addRecordToDatabase”的调用。如果我删除对方法的调用并替换方法中的实际代码,则错误消失。导致这种情况的原因是什么?

// do something useful with the data (like, enter it into the d/b)
    SQLiteDB *db = [SQLiteDB sharedSQLiteDB];
    [db addRecordToDatabase:(NSString*) symbol.data andTypeName: (NSString *) symbol.typeName];


//---------------------    addRecordToDatabase    ----------------------|
- (void)addRecordToDatabase:(NSString*)data andTypeName: (NSString *)typeName  {

    NSString *insertCommand = [NSString stringWithFormat:@"INSERT INTO s64Data (CARD_ID, CARD_NAME, CODE_VAL) VALUES ('/%s', '/%@', '/%s')",
                           data, @"Test", typeName];

    if(sqlite3_open_v2(cDatabasePath, sharedSQLiteDB, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK) {


    }
}

这是控制台输出:

The Debugger has exited with status 0.
[Session started at 2011-04-30 06:14:36 -0700.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1510) (Fri Oct 22 04:12:10 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "--host=i386-apple-darwin --target=arm-apple-darwin".tty /dev/ttys001
Loading program into debugger…
sharedlibrary apply-load-rules all
Program loaded.
target remote-mobile /tmp/.XcodeGDBRemote-778-28
Switching to remote-macosx protocol
mem 0x1000 0x3fffffff cache
mem 0x40000000 0xffffffff none
mem 0x00000000 0x0fff none
run
Running…
[Switching to thread 11779]
[Switching to thread 11779]
continue
2011-04-30 06:14:50.183 PointPeek[137:707] error: (null)
Program received signal:  “EXC_BAD_ACCESS”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.2 (8H7)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
(gdb) 

3 个答案:

答案 0 :(得分:1)

这一行

[db addRecordToDatabase:(NSString*) symbol.data andTypeName: (NSString *) symbol.typeName];

毫无意义。参数类型在方法声明中,而不是方法调用。

[db addRecordToDatabase:symbol.data andTypeName:symbol.typeName];

会更有意义。

答案 1 :(得分:1)

嗯,你有什么尝试吗?如果你有条不紊地进行调试,这应该是一个非常简单的调试。

问题:

  1. 是否定义了db?它是SQLiteDB的“真实”实例还是以某种方式腐败?是处于正常状态还是返回错误状态?

  2. symbol.datasymbol.typeName有哪些类型?你真的需要把它们投射到NSString吗?

  3. symbol.datasymbol.typeName的价值是多少?他们是明智的还是非零的?

  4. 哪一行addRecordToDatabase::导致崩溃?

  5. 如果不是第一行,insertCommand的价值是多少?

  6. 我的猜测是你的崩溃是因为你传递NSString,然后告诉[NSString stringWithFormat:]这些相同的类型是char*%s)。您需要将%@用于对象。 NSString与普通C字符串不同。结果很难预测,因为它可能取决于堆栈的内容。听起来很可能......

答案 2 :(得分:0)

EXC_BAD_ACCESS是内存管理不良的症状。您尝试访问的对象之一(dbsymbol或可能symbol.data)可能已从内存中删除或最初未正确分配。

您可以通过在相关行和调试中设置断点来找出哪一个。我的钱在symbol。因此,在创建symbol时,请尝试添加retain调用,例如:

Symbol *symbol = [Symbol symbolWithData:blah] retain];
... (make db call here)
[symbol release];

其他一些建议:

  1. 确保您设置了XCode breakpoints are tripped any time an NSException is thrown
  2. 按照this Stack Overflow post
  3. 中的说明使用NSZombiesEnabled
相关问题