Smart Home
Smart Home

Reputation: 811

when are objects released in objective-C program

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

  1. Creates 3 classes Person, Employee (that inherits from person) and Assets (Employee has an instance variable that points to Assets object)

  2. It then creates 2 NSMUtableARrays

    • Array 1: employees: Array of Employee objects (some which point to 0 Asset objects, some to 1 and some to 2 asset objects)
    • Array 2: allAssets: Array os Asset objects (one entry for each Asset object created)
  3. 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

  4. 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

Answers (3)

zaph
zaph

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

newacct
newacct

Reputation: 122439

Why is this happening? I thought autorelease would kick in only after return happens?

When you autorelease an object, it will be released 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

Nick
Nick

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

Related Questions