user2083364
user2083364

Reputation: 764

iOS, shared_ptr lifecycle in objective-c code?

I use MonkSVG library which uses boost::shared_ptr type (as I understand std::shared_ptr works in this same way).

As I understand I don't need to free memory from it by myself.

I created a separate UIViewController with code from library example that defines shared_ptr variable and push/pop this view controller. But it seems something wrong with memory or this variable doesn't work in way I think. The problem is in the destructor of shared_ptr object:

I think that shared_ptr object's destructor must be called each time I pop the view controller with it. But it is called when I assign to this variable another instance only. It looks something like the following:

push/pop   |   shared_ptr   |   usual object

push   |   -   |   -

pop   |   -   |   destructor

push   |   destructor   |   -

pop   |   -   | destructor

etc.

Is it its normal behaviour?

Upvotes: 1

Views: 365

Answers (1)

jbat100
jbat100

Reputation: 16827

Popping (I assume you mean in a UINavigationController) a UIViewController will not necessarily deallocate it (you can override its dealloc method to check that). The UIViewController instance will remain alive as long as you keep a strong reference to it.

However when you re-assign your UIViewController variable, you lose the strong reference to the old instance (if you a using ARC), which causes it to be deallocated, which then destroys any shared_ptr instance variables.

Upvotes: 1

Related Questions