×
Namespaces

Variants
Actions

Using LeaveIfNull correctly

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Compatibility
Platform(s): S60
Article
Created: User:Technical writer 1 (26 Nov 2007)
Last edited: lpvalente (28 Jul 2014)

Description

The following method from class User:

 static IMPORT_C TAny* LeaveIfNull( TAny *aPtr );


always leaves with reason code KErrNoMemory if the specified pointer is NULL. By default, this results in "Memory low, try closing some applications" error dialog to be displayed to the user. Therefore, this method should be used only in low memory situations.

Solution

Ensure that LeaveIfNull() is only used when the root cause really is insufficient amount of available RAM. Do not use this for checking other cases, such as "Item not found" or "function argument is NULL". Wrong usage causes unnecessary and misleading "Out of memory" notes to the user, even though the error is caused by something else.

For example, in the following code the cause of error is probably something else than low memory:

 ptr = iStore.GetPointerToItem( index );
 User::LeaveIfNull( ptr ); // leaves with KErrNoMemory if ptr is NULL


The correct implementation in this case would be:

 ptr = iStore.GetPointerToItem( index );
 if( !ptr )
   {
   User::Leave( KErrNotFound );
   }


Another example: Leaving function that takes a pointer as argument:

 void DoesNotAcceptNullL( type* aArg )
   {
   User::LeaveIfNull( aArg );
   ...
   }

As the real reason for passing a NULL pointer as argument is not known, LeaveIfNull should not be used. Instead, select a leave code that better describes the error:

 void DoesNotAcceptNullL( type* aArg )
   {
   if ( !aArg )
     {
     User::Leave( KErrArgument );
     }
   ...
   }
This page was last modified on 28 July 2014, at 20:45.
70 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.

×