Bitmap Duplicate

Login to reply to this topic.
Wed, 2008-04-23 10:33
Joined: 2008-04-15
Forum posts: 7

hello

I am using the following code

User::LeaveIfError(mybitmap->Duplicate(myhandle));
User::LeaveIfError(my2bitmap->Duplicate(my2handle));

and after this i am doing

CleanupStack::PopAndDestroy (2, mybitmap )

Do i need to free myhandle too ?


Wed, 2008-04-23 10:45
Joined: 2005-04-13
Forum posts: 90
Re: Bitmap Duplicate

duplicate does not push the object on CleanupStack, then why do you need to Pop it out.

Or in case u think that CleanupStack::Pop is required, please post more code snippet.

Regards,
Jupitar


Jupitar

Wed, 2008-04-23 12:31
Joined: 2008-04-15
Forum posts: 7
Re: Bitmap Duplicate

Thanks Jupitar

some more code..below

void MyBitController::SetImage(TInt aRef, TInt myBitmaphandle, TInt myBitmap2handle)
{

CFbsBitmap* myBitmap = new (ELeave) CFbsBitmap;
CleanupStack::PushL(myBitmap);
CFbsBitmap* my2Bitmap = new (ELeave) CFbsBitmap;
CleanupStack::PushL(my2Bitmap);

User::LeaveIfError(myBitmap->Duplicate(myBitmaphandle));
User::LeaveIfError(my2Bitmap->Duplicate(myBitmap2handle));

TSize bitmapSize = myBitmap->SizeInPixels();

TUint16* rgb = new (ELeave) TUint16[iWidth * iHeight];

//
< ............SOME CODE .......... >

my2Bitmap->Reset();
myBitmap->Reset();

CleanupStack::PopAndDestroy(2, myBitmap); // my2Bitmap, myBitmap

//
< ............SOME CODE .......... >

delete [] rgb;

}

My question is do I need to free myBitmaphandle and myBitmap2handle ?

Wed, 2008-04-23 17:07
Joined: 2003-12-05
Forum posts: 586
Re: Bitmap Duplicate

If you have created the bitmaps before, and passed the handles into this function, you should make sure the bitmap objects are deleted.

See ~CFbsBitmap documentation - it calls CFbsBitmap::Reset, which:

Releases the bitmap's handle from the font and bitmap server and decrements its access count. The server-side bitmap is only deleted when the access count for the bitmap decrements to zero.

So if you do not call delete on all the bitmap objects (also the Duplicated ones), the bitmap is not destroyed.

Thu, 2008-04-24 12:55
Joined: 2008-04-15
Forum posts: 7
Re: Bitmap Duplicate

Hello Andreas

I agree with you, but who is going to delete this duplicate object ? In my code I have taken care of deleting the bitmap objects.

my2Bitmap->Reset();
myBitmap->Reset();

CleanupStack::PopAndDestroy(2, myBitmap); // my2Bitmap, myBitmap

If application is calling this function, application should delete this or I should take care of this on my code.

Thu, 2008-04-24 13:09
Joined: 2003-12-05
Forum posts: 586
Re: Bitmap Duplicate

This must be designed and the decision followed. If you do not develop all this code, you need to talk to the other developers. Or see if they have documented their part.

Thu, 2008-04-24 15:26
Joined: 2004-11-29
Forum posts: 1133
Re: Bitmap Duplicate

If what is send to you is just a bitmap handle, then the lifetime of the bitmap it is not your problem to care about.

The original owner of the bitmap is whoever send you the handle, and this guy is the one responsible for freeing the bitmap when he decides he is done with it.

It is not a decision you can make in your code, or everything would break down. (What would happen if the original owner tried to use the bitmap after you have deleted it?)

After you have "Duplicate":ed the image, there is two owners, the original owner and you.

You have fulfilled all obligations you need to if you simply delete your own CFbsBitmap object and thus decrease the access counter to just one owner again.

Fri, 2008-04-25 08:14
Joined: 2008-04-15
Forum posts: 7
Re: Bitmap Duplicate

Thanks to both of you.

If the original owner of the bitmap is whoever sends the handle is not freeing the bitmap when he decides he is done with it. I should see a memory leak. Right ? if it is there how to find out the memory leak in this case.

Cheers

Mon, 2008-04-28 15:55
Joined: 2004-11-29
Forum posts: 1133
Re: Bitmap Duplicate

Probably not actually.

Only if the original owner is in the same thread.

Bitmaps are shared resources, and are stored on a special heap, separate from the heap of your application.
This heap is controlled by the "font and bitmap server" and accessed through CFbs*-classes.
The only thing kept in the memory of your own heap is the CFbsBitmap object itself, not the bitmap data.
The bitmap data has an access counter keeping track of how many CFbsBitmap objects exists in the system referencing the bitmap data.
When you call "Duplicate" the counter is increased, and if you call either "Reset" or the destructor of the CFbsBitmap, the counter is decreased.
When it reaches 0, the bitmap is removed.

So if the original owner is in some other thread, sending you the bitmap handle to duplicate over some kind of IPC/ITC, then you will not notice in your thread if he does not release it.

He will get a memory leak in his thread though, if he forgets to free his CFbsBitmap object.
He also must make sure that the destructor of the CFbsBitmap object is called properly (easiest doable by simply following conventions), or call "Reset" manually on it, so that the font and bitmap server know no one needs the bitmap any longer.

But... again, that is the headache of the original owner, not the guy who just receives a bitmap handle to duplicate.

  • Login to reply to this topic.