更新的答案
我想我知道你想做什么。您想从 SQL 中获取一组行并将其存储在一个数组中。
从 SQL 获取数据行并将其存储到类实例变量数组中的一种技术是不返回临时数组,而是将类实例变量数组作为引用传递给您的方法并直接修改数组。
所以代替这个伪代码
-(NSMutableArray *)doSomething
{
NSMutableArray *tempArray;
while (DB select statement has found rows)
{
CockTail *objCT = [[CockTail alloc] init];
objCT.name = @"...";
objCT.price = @"...";
[tempArray addObject:objCT];
[objCT release];
}
return [tempArray autorelease];
}
// class instance variable array
instanceVarArray = [[NSMutableArray alloc] init];
instanceVarArray = [self doSomething]; // here is where you confusion arise
你可以这样做:
-(void)doSomething:(NSMutableArray *)paramArray
{
// remove previously fetched data
[paramArray removeAllObjects];
SQL select statement
while(has rows)
{
CockTail *objCT = [[CockTail alloc] init];
objCT.name = @"...";
objCT.price = @"...";
// NOTE: we are directly modifying our class instance variable array
// here since it was passed by reference :D
// and so there is no need to worry about releasing the array
[paramArray addObject:objCT];
[objCT release];
}
}
// Now all you do is pass in your class instance variable array
instanceVarArray = [[NSMutableArray alloc] init];
[self doSomething:instanceVarArray];
原始答案
嗯,也许我错了,但是当您从 FMDBDatabaseAccess 中为数组分配某些内容时,您实际上不是在第一行丢弃了“alloc init”吗:
// LINE 1: this instance of NSMutableArray here is allocated
alarmTableArray = [[NSMutableArray alloc]init];
// LINE 2
FMDBDatabaseAccess *db = [[FMDBDatabaseAccess alloc] init];
// LINE 3:this line here essential breaks the pointer link point to the NSMutableArray instance on line 1
alarmTableArray = [db getAlarm];
现在除非你这样做
// LINE 4
[alarmTableArray retain];
否则,您的 alarmTableArray 永远不会被分配(因为您覆盖了指针链接)。结果,正如您的分析器告诉您的那样,您导致了内存泄漏。
现在做一个发布会给你那个 EXEC_BAD_ACCESS
我认为你想要做的是:
alarmTableArray = [[NSMutableArray alloc]init];
FMDBDatabaseAccess *db = [[FMDBDatabaseAccess alloc] init];
// this now uses the setter method (mutator method generated by @property) to do the copy
self.alarmTableArray = [db getAlarm];