How to share CFbsBitmap instances between threads?

Login to reply to this topic.
Mon, 2005-08-01 14:52
Joined: 2004-10-01
Forum posts: 35
Hi Experts,
I have a problem sharing CFbsBitmap objects between threads. Symabian's SDK document says to use "Duplicate()" function of CFbsBitmap class for this. But i am unable to do it. I tried using Duplicate in two ways but it does not help. Below is the code.

1.
    MAIN THREAD:

        iImage=(CFbsBitmap*)new CFbsBitmap;   //iImage is a member variable of CExampleAppUi
       TInt Err = iImage->Create(cif_size,EColor64K);
       if(Err != KErrNone)
      {
       RDebug::Printf("\nUnable to allocate mem for image--Error = %d", Err);
       }
      Create a new thred here and pass "this" pointer. "this" pointer is pointing to CExampleAppUi object

   CHILD THREAD:
     CFbsBitmap* Image = STATIC_CAST(CExampleAppUi*,param)->iImage; //param is parameter received by the static thread func.
     CFbsBitmap*   iBitmap = new (ELeave) CFbsBitmap();
     TInt temp  = Image->Handle();
     RDebug::Printf("Got the handle  %d", temp); //Getting the handle correctly
     temp =    iBitmap->Duplicate(temp);
    RDebug::Printf("Duplicated the handle with the return value %d", temp);

  Duplicate returns with the value -34, which ,means "KErrCouldNotConnect" . So i am not able to duplicate the handle this way.

2.
   MAIN THREAD

       iImage=(CFbsBitmap*)new CFbsBitmap;   //iImage is a member variable of CExampleAppUi
       TInt Err = iImage->Create(cif_size,EColor64K);
       if(Err != KErrNone)
      {
       RDebug::Printf("\nUnable to allocate mem for image--Error = %d", Err);
       }
      iDuplicateImage = new (ELeave) CFbsBitmap();   // iDuplicateImage is member variable of CExampleAppUi
     TInt temp  = iImage->Handle();
     RDebug::Printf("Got the handle  %d", temp);          // getting the handle correctly
     temp =    iDuplicateImage->Duplicate(temp);
    RDebug::Printf("Duplicated the handle with the return value %d", temp);   //Duplicate returns 0 means success in duplicating

     Create a new thred here and pass "this" pointer. "this" pointer is pointing to CExampleAppUi object

   CHILD THREAD:
      CFbsBitmap* Image = STATIC_CAST(CExampleAppUi*,param)->iDuplicateImage; //param is same as above
      Image->LockHeap(ETrue);
     Now it gives me KERN-EXEC 0 error.

I am unable to solve this issue. What might be the reason?
Am i doing any mistake??

Please help me in this regards,

Thanks
Pinky

   

Tue, 2005-08-02 04:29
Joined: 2003-04-01
Forum posts: 142
Re: How to share CFbsBitmap instances between threads?
hi

General rule with symbian OS is that Handles are thread specific, so you can not share them between threads. anyway I also remember reading about bitmaps, that the memory area for them is implemented in global chunk,  this is for the drawing access, didn't really read it too well so can not remember details. Anyway, if it is really implemented as such, then there should be ways to access this data from other processes as well.

Anyway, in case you are running both of the threads in same process, you could share pointers, so maybe you could workaround using the pointer to the data area of the bitmap image.

Have to say that I haven't really done too much threads programming, so can be considered a rookie on this issue.

yucca
Tue, 2005-08-02 07:24
Joined: 2005-03-31
Forum posts: 173
Re: How to share CFbsBitmap instances between threads?
In 2nd case, i guess error occurs at
Image->LockHeap(ETrue);
If it is the case, then it means
Image is pointing to Null. In other words
iDuplicateImage is not the one from Main thread.

Tue, 2005-08-02 08:26
Joined: 2004-10-01
Forum posts: 35
Re: How to share CFbsBitmap instances between threads?
Yes, It panics when i call Image->LockHeap(ETrue) but Image is not pointing to NULL. I have tried with following prints.

IN MAIN THREAD:

   RDebug::Printf("Duplicated image %x",   iDuplicateImage);

   RDebug::Printf("DataAddress for Original image %x",   iImage->DataAddress());

   RDebug::Printf("DataAddress for Duplicated image %x",   iDuplicateImage->DataAddress());

    // First Print gives non-zero, meaning that iDuplicateImage is a valid object. The second and 3rd prints both give the same  address,meaning that both are pointing to same address which had been allocated for iImage in main thread.

IN CHILD THREAD:

   RDebug::Printf("Image in thread %x",   Image);

This print in the child thread   prints the same value as the first print is printing in the main thread. So It is same as iDuplicateImage. I can not print the address pointed by this because for that i will have to LockHeap first, which is panicing.

Thx
Pinky


Quote from: SymbianSandy
In 2nd case, i guess error occurs at
Image->LockHeap(ETrue);
If it is the case, then it means
Image is pointing to Null. In other words
iDuplicateImage is not the one from Main thread.


Tue, 2005-08-02 08:53
Joined: 2003-04-01
Forum posts: 142
Re: How to share CFbsBitmap instances between threads?
the error is "KERN-EXEC 0" not "KERN-EXEC 3", so instead of identifying memory related problems, it is saying that the handle is invalid. Thus check the image->Handle(), value before proceeding to work with the image.

yucca
Tue, 2005-08-02 09:22
Joined: 2004-10-01
Forum posts: 35
Re: How to share CFbsBitmap instances between threads?
Hi Yucca,
I have already tried the Image->Handle(). Its same as in the main thread. So its not a problem with the handle.

Thx
Pinky
Tue, 2005-08-02 10:28
Joined: 2003-04-01
Forum posts: 142
Re: How to share CFbsBitmap instances between threads?
you propably missed what I said earlier: that is that the handles are thread specific. So having a same handle value in different threads, is meaning that they are for different objects... thus the value in second thread, is not having a valid object for the handle number, thus you get invalid handle error.


yucca
Tue, 2005-08-02 12:09
Joined: 2004-10-01
Forum posts: 35
Re: How to share CFbsBitmap instances between threads?
Ohhh that could be the reason. But then, How can i solve this problem??

Pinky
Wed, 2005-09-14 17:18
Joined: 2005-09-13
Forum posts: 1
Re: How to share CFbsBitmap instances between threads?
Hi,

I am also facing the same issue. i.e After duplicating the CFbsBitmap, LockHeap() is panicking with KERN-EXEC 0.
Handle numbers are same for both pointers of CFbsBitmap before and after Duplicating it.

Please help me in this regard.

Thanks
nsat
Wed, 2005-09-14 18:29
Joined: 2004-11-29
Forum posts: 1271
Re: How to share CFbsBitmap instances between threads?
CFbsBitmap handles are _not_ thread specific.
The whole point of them is to share them between threads (or even processes).

I don't know where you got the information that handles are usually thread specific.

The main reason for handles to exist in any os is otherwise because you want to share them between threads and processes. You have a handle because you can't share pointers, and you ask the system for access to the handle, which then gives you this and a pointer into the process own adress space where it can be found.

Here is also a snippet from the Symbian docs:

---
Handle-numbers
A handle is a way in which a thread or process can identify an object that is owned or managed by another thread or process. Such objects are Kernel side objects.

A handle uses a number, the handle-number, to identify the associated Kernel side object. The handle, an instance of a RHandleBase derived class, encapsulates the handle-number.
----

I know you in some cases in symbian use a pointer for a handle, when its only used in the same thread, but that is an exception, not a rule...


Wed, 2005-09-14 21:48
Joined: 2004-07-17
Forum posts: 110
Re: How to share CFbsBitmap instances between threads?
There's quite a bit of confusion here I reckon. Try the following (it might work!)

1. The handle you get from CFbsBitmap::Handle() gives you a handle number for the bitmap and this is used by the Font and Bitmap Server to identify the bitmap. Use the handle to create a duplicate bitmap in the second thread (pass the handle between the threads, not the CFbsBitmap pointer!)

2. The second thread also needs to have a session with the FBs server, otherwise you can't do anything with bitmaps. I think this explains the -34. Call RBsSession::Connect() in the second thread before attempting to duplicate the bitmap or lock the heap.

Wed, 2005-09-14 23:35
Joined: 2005-09-14
Forum posts: 2
Re: How to share CFbsBitmap instances between threads?
i tried duplicating CFbsbitmaps between threads. On the thread that i loaded the bitmap, i can duplicate the bitmap with handle number of the bitmap i loaded. However, trying to duplicate the bitmap using the same handle number on a different thread i get a error -19. Any ideas why? I did connect to the Rfbs server on both threads.
  • Login to reply to this topic.