Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Avoiding memory leaks during transfer of ownership

From Wiki
Jump to: navigation, search


Article Metadata
Compatibility
Platform(s): S60
Article
Created: User:Technical writer 1 (26 Nov 2007)
Last edited: hamishwillee (30 May 2013)

Description

The following code demonstrates a common pattern for creating memory leaks:

 User::LeaveIfError(bar.TakesOwnershipIfSuccessful(CFoo::NewL()));

If the construction of CFoo succeeds, but then the function call that is supposed to take ownership of it fails, then the newly-created CFoo instance is orphaned.

Solution

The only way to do this safely is to use the cleanup stack:

 CFoo* foo = CFoo::NewL();
 CleanupStack::PushL(foo);
 User::LeaveIfError(bar.TakesOwnershipIfSuccessful(foo));
 CleanupStack::Pop(foo);


The most common situations where these kind of leaks occur are array operations such as Append() or Insert().

This page was last modified on 30 May 2013, at 04:36.
75 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×