作者:手机用户2602930515 | 来源:互联网 | 2023-01-20 09:58
Imtryingtobuildaclassforhandlingallsqlite3workandIveencounteredanEXC_BAD_ACCESSwhi
I'm trying to build a class for handling all sqlite3 work and I've encountered an EXC_BAD_ACCESS which I just can't explain. I am new to Objective-C development and memory management in general so I apologize if this is a stupid question.
我正在尝试构建一个用于处理所有sqlite3工作的类,我遇到了一个我无法解释的EXC_BAD_ACCESS。我是Objective-C开发和内存管理的新手,所以我很抱歉这是一个愚蠢的问题。
When initializing the class I get the path to the database file and keep it around:
初始化类时,我获取数据库文件的路径并保持它:
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
databasePath = [[[documentPaths objectAtIndex:0] stringByAppendingPathComponent:@"Database.sql"] retain];
Then I try to use it and it crashes on me:
然后我尝试使用它,它崩溃了我:
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // crashes
The odd thing is that the exact same line in a different function works perfectly. I tried adding the variable initialization to above the line, but to no avail:
奇怪的是,不同功能中的完全相同的线路完美运行。我尝试将变量初始化添加到行上方,但无济于事:
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
databasePath = [[[documentPaths objectAtIndex:0] stringByAppendingPathComponent:@"Database.sql"] retain];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // crashes
In both cases the retain count is 2.
在这两种情况下,保留计数为2。
However, putting any form of static text in there works fine:
但是,在其中放置任何形式的静态文本都可以正常工作:
databasePath = @"I have a balloon";
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // fine
// or
if (sqlite3_open([@"APPLES!" UTF8String], &database) == SQLITE_OK) { // fine
A little more experimentation revealed that it was the UTF8String function that was crashing, but I don't understand why.
多一点实验表明,UTF8String函数崩溃了,但我不明白为什么。
Edit: Even more experimentation resulted in me being able to call the UTF8String function but not even use the results:
编辑:更多的实验导致我能够调用UTF8String函数,但甚至不使用结果:
const char * test = [databasePath UTF8String];
NSLog(@"%@", [NSString stringWithUTF8String:test]); // fine
if (sqlite3_open(test, &database) == SQLITE_OK) { // fails
Edit: Messed around even more and discovered that the problem was in a parameter that I was passing to the function. I had breakpoints all over the place so I was 100% sure that the point it fails is at the line I specified, a line which doesn't use the passed variable, but somehow it caused everything to fail. Sometimes. I have no idea how it went wrong, but I rewrote the entire thing, changed my function call and now it works.
编辑:围绕更多,并发现问题是在我传递给函数的参数中。我到处都有断点,所以我100%确定它失败的点是在我指定的行,一行不使用传递的变量,但不知怎的,它导致一切都失败了。有时。我不知道它是怎么出错的,但我重写了整个事情,改变了我的函数调用,现在它工作了。
Thanks to all who answered.
感谢所有回答的人。
5 个解决方案
0
Try a copy instead of a retain on the variable - it'll make an immutable copy of the string so it shouldn't go changing or disappearing.
尝试复制而不是保留变量 - 它将生成字符串的不可变副本,因此它不应该改变或消失。
EDIT::
编辑::
What happens when you simply type
只需键入即会发生什么
if (sqlite3_open[@"myDatabase.sqlite" UTF8String], &database) == SQLITE_OK) {
Also are you sure that the
你还确定吗?
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
databasePath = [[[documentPaths objectAtIndex:0]
stringByAppendingPathComponent:@"Database.sql"] retain];
is actually returning where the database is?
实际上是返回数据库所在的位置?
The code I use to grab the database from the app directory where kFilename is the database file is:
我用来从app目录中获取数据库的代码是kFilename是数据库文件:
- (NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
and then to use it I do:
然后使用它我做:
if(sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK){
This works on both my iPod touch and the iPhone simulator.
这适用于我的iPod touch和iPhone模拟器。
EDIT2::
EDIT2 ::
I've just used your above method and it seems to work here - I know it sounds stupid but have you tried rebooting? Its possible something strange is happening with the simulator
我刚刚使用了你的上述方法,它似乎在这里工作 - 我知道它听起来很愚蠢,但你尝试重启吗?它可能在模拟器中发生了奇怪的事情