In the following code snippet:
NSMutableArray *tmpItemsArray =[[[NSMutableArray alloc] init] autorelease];
while (sqlite3_step(fetch_statement)==SQLITE_ROW){
int pk = sqlite3_column_int(fetch_statement,0);
Item_A *itemA = [[Item_A alloc] getItemA:pk fromDatabase:db];
// calculate and set a property of itemA
[itemA setValue:xValue forkey:xKey];
// insert into array
[tmpItemsArray addObject:itemA];
[itemA release];
}
sqlite3_reset(fetch_statement);
// for each ItemA in array
// update itemA in database
for (Item_A *eachItemA in tmpItemsArray) {
[eachItemA updateItemAInDatabase:db];
}
When I add object to tmpItemsArray using addObject: method, the object at memory address pointed by itemA is added to the array. In other words, an object in tmpItemsArray is pointing to the same memory address as is pointed by ItemA.
Now when [itemA release] is executed, to release the memory –
would tmpItemsArray have objects pointing to invalid memory
or
does [itemA release] only releases the hold(in a way) itemA has over this memory?
releasedoesn’t actually cause memory to be freed. Instead it decrements a count that represents how many times some part of the code has retained the object. The act of allocating the object is equivalent to one retain and adding it to the array is another. Yourreleasebalances theallocand, at that point, the array’s retain is keeping it from being deallocated.When the array goes away, barring other events, it will release its contents. Since it’s an autoreleased array, that’s likely to happen on the next iteration of the program’s event loop.