Connecting Groups
Back to SiteGroups!

SpringSters - Mobile Savvy Bunch

The power of small

Memory Management on Mobile Platform

 

When I was a school kid, I never actually worried about the resources I was using or occupying. I always finished my pocket money before the month end. I was wasting a lot of resources and the reason for that I think was that I didn’t realize where the importance and also the source of resources! But after finishing my schools I never remained careless about my resources. I would attribute the change to learning C and C++ in my college. “Isn’t it funny?” JYes, the memory management in C and C++ encouraged me to handle my resources.

Then came mobile platforms in my life. The best platform to learn doing a lot of things with very less resources. Every chunk of memory you allocate, needs a reason to do so. Gives another dimension to your resources handling skills.

In last few years I have come across a few mobile technologies. Started with Symbian switched to Palm OS for some amount of time and then back to Symbian a little bit of Windows mobile here and there for a change of taste for few days. I came across different memory management techniques, ways to handle exceptions. And of course distinguishing features for every platform. For Palm its “Locking and unlocking of memory chunk”. This I could talk about in one of the later posts. This one is about my first love Symbian and the bunch of features that distinguish Symbian

Two of the Symbian features I like most are:

  1. Two phase construction
  2. Cleanup stack

Two phase construction:

How do you feel when you just start to construct something and it fails? Some things don’t happen as per the expectations, it’s failure on the planning side. Lets take an example: you start building your house and during construction you notice that the carpenter you booked for your furniture didn’t give you proper estimates for resources and your construction fails .. or at least gets delayed for few weeks or months. But in this scenario you can recover by finding another carpenter. But while programming what if the code in your constructor fails? Lets take an example -

class Person

{

char* name;

char* surname;

public:

// constructor and destructor

Person();

~Person();

}

Now let’s say we allocate these two member variables in a constructor as follows:

Person()

{

name = new char[100];

surname = new char[200];

}

Now the allocation for “name” goes well according to plan. And due to unavailability of a free 200 bytes the allocation for “surname” fails. Now what will happen to the “name” which is already allocated? Its orphaned and finally a memory leak in my application. The constructor of any class should not throw any exceptions ( Leave in Symbian ).

According to me, the best way to handle this situation would be a Two phase construction. It’s a method of creating an object in two phases. First phase would be a constructor and other is say “Construct” method. Let’s take above example class and add a two phase constructor to it.

Class Person

{

Person();

bool Construct()

public:

static Person* Create();

~Person()

}

Person* Person::Create()

{

Person* me = new Person();

if(Person == NULL)

{

return NULL;

}

if( me->Construct() == false)

{

delete me;

return NULL;

}

return me;

}

bool Person::Construct()

{

name = new char[100];

// check for allocation

if(name == NULL )

{

// allocation failed

return FALSE;

}

surname = new char[200];

if(surname == NULL )

{

// allocation failed

// delete previously allocated memory

delete name;

name = NULL;

return FALSE;

}

// everything is successful it means my object is properly allocated

return TRUE;

}

 

This technique of allocation of memory is very impressive for mobile platform because it handles the failure of object construction. I would love to see non wireless platform adopting similar techniques to ensure cleaner object construction.

Be back with the next post on “Cleanup Stack” soon, remember thats other feature I like the most in Symbian.

- Kiran

©2007 - 2008 MangoSpring. All Rights Reserved.