Why are assets deallocating?


#1

I’ve got main.m on pages 130 and 131 running and the output makes sense. I wanted to see what would happen if allAssets wasn’t set to nil so I commented out that line.

// allAssets = nil; employees = nil;

What I expected is that none of the assets would be deallocated because the allAssets array is still there. But what happens is a very orderly deallocation of the employees array (indexes 0 to 9) followed immediately by a very orderly deallocation of the assets array (indexes 0 to 9).

2012-01-12 15:17:27.233 BMITime[1700:707] deallocating <Employee 0: $0 in assets> 2012-01-12 15:17:27.233 BMITime[1700:707] deallocating <Employee 1: $153 in assets> 2012-01-12 15:17:27.276 BMITime[1700:707] deallocating <Employee 2: $119 in assets> 2012-01-12 15:17:27.276 BMITime[1700:707] deallocating <Employee 3: $68 in assets> 2012-01-12 15:17:27.277 BMITime[1700:707] deallocating <Employee 4: $0 in assets> 2012-01-12 15:17:27.277 BMITime[1700:707] deallocating <Employee 6: $119 in assets> 2012-01-12 15:17:27.277 BMITime[1700:707] deallocating <Employee 7: $34 in assets> 2012-01-12 15:17:27.278 BMITime[1700:707] deallocating <Employee 8: $0 in assets> 2012-01-12 15:17:27.278 BMITime[1700:707] deallocating <Employee 9: $136 in assets> 2012-01-12 15:17:27.279 BMITime[1700:707] deallocating <Laptop 0: $0, unassigned> 2012-01-12 15:17:27.279 BMITime[1700:707] deallocating <Laptop 1: $17, unassigned> 2012-01-12 15:17:27.279 BMITime[1700:707] deallocating <Laptop 2: $34, unassigned> 2012-01-12 15:17:27.280 BMITime[1700:707] deallocating <Laptop 3: $51, unassigned> 2012-01-12 15:17:27.280 BMITime[1700:707] deallocating <Laptop 4: $68, unassigned> 2012-01-12 15:17:27.280 BMITime[1700:707] deallocating <Laptop 5: $85, unassigned> 2012-01-12 15:17:27.281 BMITime[1700:707] deallocating <Laptop 6: $102, unassigned> 2012-01-12 15:17:27.281 BMITime[1700:707] deallocating <Laptop 7: $119, unassigned> 2012-01-12 15:17:27.281 BMITime[1700:707] deallocating <Laptop 8: $136, unassigned> 2012-01-12 15:17:27.282 BMITime[1700:707] deallocating <Laptop 9: $153, unassigned>

I see that they are all unassigned but I thought that the Asset pointers in the allAssets array would be enough to keep the assets around. What am I missing?


#2

The Assets in allAssets are deallocated automatically because they’re in the autorelease pool, whereas the pointer in the Asset class itself (outside of main.m) is NOT in the autorelease pool.

At least that’s my guess as to what’s happening - anything in main is going to be autoreleased (note the @autorelease bracketing), i.e. the allAssets array is set to autorelease because of its location in main. It’s different for objects that contain pointers within their own class definitions, because those pointers are not placed in the autorelease pool.


#3

There’s a very similar thread in Chapter 21’s forum that I’d recommend that you check out: viewtopic.php?f=153&t=3173