【发布时间】:2010-09-28 09:52:27
【问题描述】:
我有两个问题
1) 我的应用在前几次运行时在设备上运行良好。然后它在第一个屏幕弹出(Tab BAr)后崩溃。如果我将设备连接到我的 MAC,然后运行设备应用程序,它就可以工作(不是调试模式)。
我检查了崩溃日志,它崩溃了“EXC_BAD_ACCESS (SIGSEGV)”的 cos 并且 Thread0 崩溃了。错误是 NSAutorelease 释放了一个已释放的对象。
2) 我在模拟器上使用仪器运行应用程序。它在这个函数调用上显示了很多泄漏。 这是一个示例代码。当我使用 Instruments 运行它时,它会在“setObject”行显示泄漏。
//类A-NSObject的子类
+(NSMutableDictionary *)Hello {
NSMutableDictionary *dctONE = [[NSMutableDictionary alloc]initWithCapacity:0];
NSMutableArray *arrKeys = [[NSMutableArray alloc] initWithCapacity:0];
[arrKeys addObject:@"Object1"];
[arrKeys addObject:@"Object2"];
[dctONE setObject:[NSString stringWithFormat:@"dsfsdf"] forKey:[arrKeys objectAtIndex:0]];
[dctONE setObject:[NSString stringWithFormat:@"dsfsdf"] forKey:[arrKeys objectAtIndex:1]];
[arrKeys release];
return dctONE;
}
/// B类
-(void)some_Function {
NSMutableDictionary * dct = [A Hello]; //all declarations are done
//do stuff with dct
[dct release];
}
为什么它会在“setObject”处泄漏??我正在正确地释放一切,对吗?唯一的事情是 [NSString stringWithFormat:] 但那是自动释放对吗??
这让我发疯了吗?
这两个问题有关系吗?? PS:它不会在sim卡上崩溃,奇怪的是即使我将我的设备连接到我的MAC然后在设备上测试它也不会崩溃(不是调试,直接点击设备上的应用程序)
编辑:
-(NSMutableDictionary *) ExecuteDataSet:(NSString *)strQuery
{
NSMutableDictionary *dctResult = [[[NSMutableDictionary alloc] init] autorelease];
// BOOL isSucess = FALSE;
const char *sql = [strQuery UTF8String];
sqlite3_stmt *selectStatement;
//prepare the select statement
int returnValue = sqlite3_prepare_v2(database, sql, -1, &selectStatement, NULL);
if(returnValue == SQLITE_OK)
{
sqlite3_bind_text(selectStatement, 1, sql, -1, SQLITE_TRANSIENT);
//loop all the rows returned by the query.
NSMutableArray *arrColumns = [[NSMutableArray alloc] init];
for (int i=0; i<sqlite3_column_count(selectStatement); i++)
{
const char *st = sqlite3_column_name(selectStatement, i);
[arrColumns addObject:[NSString stringWithCString:st encoding:NSUTF8StringEncoding]];
}
int intRow =1;
while(sqlite3_step(selectStatement) == SQLITE_ROW)
{
NSMutableDictionary *dctRow = [[NSMutableDictionary alloc] init];
for (int i=0; i<sqlite3_column_count(selectStatement); i++)
{
int intValue = 0;
const char *strValue;
switch (sqlite3_column_type(selectStatement,i))
{
case SQLITE_INTEGER:
intValue = (int)sqlite3_column_int(selectStatement, i);
[dctRow setObject:[NSString stringWithFormat:@"%d",intValue] forKey:[arrColumns objectAtIndex:i]];
break;
case SQLITE_TEXT:
strValue = (const char *)sqlite3_column_text(selectStatement, i);
[dctRow setObject:[NSString stringWithCString:strValue encoding:NSUTF8StringEncoding] forKey:[arrColumns objectAtIndex:i]];
break;
default:
strValue = (const char *)sqlite3_column_value(selectStatement, i);
[dctRow setObject:[NSString stringWithCString:strValue encoding:NSUTF8StringEncoding] forKey:[arrColumns objectAtIndex:i]];
break;
}
}
[dctResult setObject:[dctRow retain] forKey:[NSString stringWithFormat:@"Table%d",intRow]];
intRow ++;
[dctRow release];
}
[arrColumns release];
}
return dctResult;
}
【问题讨论】:
-
避免返回保留的对象,这是内存泄漏的大门。
-
但是我在进行调用的函数中释放了返回的对象。还不够好吗?如果没有,我如何返回分配的字典?
标签: iphone memory-leaks device nsautoreleasepool