Understanding how the deallocating works


#1

Guys,

I’m trying to understand on how the deallocating works. I found that when I remove the [randomEmployee addAssetsObject:asset]; - the deallocating for asset came first in the result.

This is the code:

        for (int i = 0; i < 10; i++) {
            // Create an asset
            ...
            ...
            ...
            // Assign the asset to the employee
            //[randomEmployee addAssetsObject:asset];  <<<<< I'm removing this
        }

This is the result:

2014-05-03 10:16:54.573 Redo-BMITime[518:303] deallocating <Laptop 0: $0 >
2014-05-03 10:16:54.575 Redo-BMITime[518:303] deallocating <Laptop 1: $17 >
2014-05-03 10:16:54.576 Redo-BMITime[518:303] deallocating <Laptop 2: $34 >
2014-05-03 10:16:54.577 Redo-BMITime[518:303] deallocating <Laptop 3: $51 >
2014-05-03 10:16:54.577 Redo-BMITime[518:303] deallocating <Laptop 4: $68 >
2014-05-03 10:16:54.578 Redo-BMITime[518:303] deallocating <Laptop 5: $85 >
2014-05-03 10:16:54.578 Redo-BMITime[518:303] deallocating <Laptop 6: $102 >
2014-05-03 10:16:54.579 Redo-BMITime[518:303] deallocating <Laptop 7: $119 >
2014-05-03 10:16:54.579 Redo-BMITime[518:303] deallocating <Laptop 8: $136 >
2014-05-03 10:16:54.580 Redo-BMITime[518:303] deallocating <Laptop 9: $153 >
2014-05-03 10:16:54.580 Redo-BMITime[518:303] Employees: (
    "<Employee 0: $0 in assets>",
    "<Employee 1: $0 in assets>",
    "<Employee 2: $0 in assets>",
    "<Employee 3: $0 in assets>",
    "<Employee 4: $0 in assets>",
    "<Employee 5: $0 in assets>",
    "<Employee 6: $0 in assets>",
    "<Employee 7: $0 in assets>",
    "<Employee 8: $0 in assets>",
    "<Employee 9: $0 in assets>"
)
2014-05-03 10:16:54.581 Redo-BMITime[518:303] Giving up ownership of one employee
2014-05-03 10:16:54.582 Redo-BMITime[518:303] deallocating <Employee 5: $0 in assets>
2014-05-03 10:16:54.583 Redo-BMITime[518:303] Giving up ownership of array
2014-05-03 10:16:54.583 Redo-BMITime[518:303] deallocating <Employee 0: $0 in assets>
2014-05-03 10:16:54.584 Redo-BMITime[518:303] deallocating <Employee 1: $0 in assets>
2014-05-03 10:16:54.584 Redo-BMITime[518:303] deallocating <Employee 2: $0 in assets>
2014-05-03 10:16:54.585 Redo-BMITime[518:303] deallocating <Employee 3: $0 in assets>
2014-05-03 10:16:54.585 Redo-BMITime[518:303] deallocating <Employee 4: $0 in assets>
2014-05-03 10:16:54.585 Redo-BMITime[518:303] deallocating <Employee 6: $0 in assets>
2014-05-03 10:16:54.586 Redo-BMITime[518:303] deallocating <Employee 7: $0 in assets>
2014-05-03 10:16:54.586 Redo-BMITime[518:303] deallocating <Employee 8: $0 in assets>
2014-05-03 10:16:54.587 Redo-BMITime[518:303] deallocating <Employee 9: $0 in assets>
Program ended with exit code: 0

This is the result of the original code:

2014-05-03 10:17:14.216 Redo-BMITime[531:303] Employees: (
    "<Employee 0: $0 in assets>",
    "<Employee 1: $153 in assets>",
    "<Employee 2: $119 in assets>",
    "<Employee 3: $68 in assets>",
    "<Employee 4: $0 in assets>",
    "<Employee 5: $136 in assets>",
    "<Employee 6: $119 in assets>",
    "<Employee 7: $34 in assets>",
    "<Employee 8: $0 in assets>",
    "<Employee 9: $136 in assets>"
)
2014-05-03 10:17:14.218 Redo-BMITime[531:303] Giving up ownership of one employee
2014-05-03 10:17:14.219 Redo-BMITime[531:303] deallocating <Employee 5: $136 in assets>
2014-05-03 10:17:14.219 Redo-BMITime[531:303] deallocating <Laptop 3: $51 >
2014-05-03 10:17:14.220 Redo-BMITime[531:303] deallocating <Laptop 5: $85 >
2014-05-03 10:17:14.220 Redo-BMITime[531:303] Giving up ownership of array
2014-05-03 10:17:14.221 Redo-BMITime[531:303] deallocating <Employee 0: $0 in assets>
2014-05-03 10:17:14.221 Redo-BMITime[531:303] deallocating <Employee 1: $153 in assets>
2014-05-03 10:17:14.221 Redo-BMITime[531:303] deallocating <Laptop 9: $153 >
2014-05-03 10:17:14.222 Redo-BMITime[531:303] deallocating <Employee 2: $119 in assets>
2014-05-03 10:17:14.222 Redo-BMITime[531:303] deallocating <Laptop 7: $119 >
2014-05-03 10:17:14.223 Redo-BMITime[531:303] deallocating <Employee 3: $68 in assets>
2014-05-03 10:17:14.223 Redo-BMITime[531:303] deallocating <Laptop 0: $0 >
2014-05-03 10:17:14.223 Redo-BMITime[531:303] deallocating <Laptop 4: $68 >
2014-05-03 10:17:14.224 Redo-BMITime[531:303] deallocating <Employee 4: $0 in assets>
2014-05-03 10:17:14.225 Redo-BMITime[531:303] deallocating <Employee 6: $119 in assets>
2014-05-03 10:17:14.225 Redo-BMITime[531:303] deallocating <Laptop 1: $17 >
2014-05-03 10:17:14.226 Redo-BMITime[531:303] deallocating <Laptop 6: $102 >
2014-05-03 10:17:14.226 Redo-BMITime[531:303] deallocating <Employee 7: $34 in assets>
2014-05-03 10:17:14.227 Redo-BMITime[531:303] deallocating <Laptop 2: $34 >
2014-05-03 10:17:14.227 Redo-BMITime[531:303] deallocating <Employee 8: $0 in assets>
2014-05-03 10:17:14.228 Redo-BMITime[531:303] deallocating <Employee 9: $136 in assets>
2014-05-03 10:17:14.228 Redo-BMITime[531:303] deallocating <Laptop 8: $136 >
Program ended with exit code: 0

Why asset is being deallocated first? Is it because of no one pointing to those those objects anymore?

Thanks.