Search code examples
c++c++buildertbitmaptcanvas

Should I unlock TBitmap canvas after locking it?


I want to draw on a local TBitmap and return it. Because I am outside the main thread, I have to lock the canvas before using it. (How threadsafe is TBitmap)

Do I ever have to unlock the canvas, or will TBitmap destructor do it for me? Is it safe to unlock the canvas in this function after drawing on the bitmap, and lock it again when I want to read the bitmap, or can the canvas get cleared if I do that?

std::shared_ptr<TBitmap> f() {
    std::shared_ptr<TBitmap> bmp(new TBitmap);
    bmp->Canvas->Lock();
    // draw on bitmap
    return bmp;
}

Solution

  • As commented by Remy:

    The bitmap destructor will NOT unlock the canvas for you. If you explicitly lock it, you must explicitly unlock it. And yes, once you unlock the canvas, the main UI thread is free to clear the bitmap resources.