Reputation: 811
I am using the Big Nerd Ranch guide to get started on Objective C. I have a question on the project talked about in chapter 20. At a high level, the project does the following
Creates 3 classes Person, Employee (that inherits from person) and Assets (Employee has an instance variable that points to Assets object)
It then creates 2 NSMUtableARrays
Following this, one of the objects in the employees array is released. This results in this Employee object being deallocated, but the asset objects linked to the Employee object are not since allAssets array owns it
Following this, I put a sleep (60) before the return 0 in main
I see that if sleep(60) is after the @autoreleasepool{} then all objects get deallocated before the 1 minute of sleep happens
But if sleep(60) happens before the closing left brace, then all objects get de-allocated after the 1 minute of sleep happens
Why is this happening? I thought autorelease would kick in only after return happens? Why is it happening before the return in the case I put the sleep outside the @autorelease{}, is it just a compiler optimization that recognizes that objects are no longer needed at this point since the only line left is sleep()?
I have uploaded the project to
https://github.com/smartiothome/BMI
Upvotes: 0
Views: 163
Reputation: 112857
Look at the sleep() documentation: "The sleep command suspends execution for a minimum of seconds."
Basically nothing is happening during sleep which is why it should never be used in a release program.
Also doing this at program exit is going to be undefined. The system is going to reclaim all memory wether the app releases it or not so the app may decide not to bother releasing anything on termination. Aside: One of the several things that the OS kernel does is handle resources, providing them to apps as request and making sure to reclaim all resources on normal app termination or by a crash.
You would be well advised to reformat the question not based on app shutdown and open a new question based on that.
Upvotes: 2
Reputation: 122439
Why is this happening? I thought autorelease would kick in only after return happens?
When you autorelease
an object, it will be release
d when the current @autoreleasepool
block (the innermost enclosing @autoreleasepool
block in the call stack at runtime) ends.
If you do not have an @autoreleasepool
block around your autorelease
in the same function, then the "current @autoreleasepool
block" is in a calling function further up the call frame, and therefore the release
happens after return from the current function.
However, in this case, you do have an @autoreleasepool
block around your autorelease
in the same function, so the release
happens at the end of that @autoreleasepool
block.
Upvotes: 0
Reputation: 2369
when control reaches the closing brace of the @autorelease
pool, that is when the current pool is told to drain
, which causes your objects to be deallocated. Your sleep call delays the drain
.
To get a better idea of whats going on, I'd recommend you take a look at how NSAutoreleasePool
worked before they added the new @
syntax. That is essentially whats happening under the covers.
Upvotes: 0