HBITMAP outside or CreateCompatibleBitmap inside?

I am using C++11 and Code::Blocks 17.12 and compiling on a 32 bit system. NO Visual Studio. NO .net. I plan to compile this later on a 64 bit system. I have two options that currently work with this code. I am trying to find out which is better.


Which is better: to use HBITMAP outside or to use CreateCompatibleBitmap inside as I show in the following?


I call the function that these are in often as I am moving images around on a window.


Option 1 Using HBITMAP outside:
1
2
3
4
5
6
   
//HBITMAP is An object handle that manages bitmap data.
   HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
   SelectObject(hdcWorkingBackBuffer, hbmBuffer);
   DeleteObject(hbmBuffer);


Option 2 Using CreateCompatibleBitmap inside.
1
2
3
4
   
// without HBITMAP
   SelectObject(hdcWorkingBackBuffer, CreateCompatibleBitmap(hdc, prc->right, prc->bottom));



I think that with Option 1 of using HBITMAP uses up more cpu and memory resources, but maybe I might be able to use hbmBuffer as a global so as to not have to create it and destroy it each time that I run the function. But I do not know what HBITMAP holds; the color palate (which will not change) or dimensions (which will not change) or the actual pixel data (which WILL change).

I think that if the image changes and if the HBITMAP holds the actual pixel data , and if the pixel data changes, then is Option 2 better?

Thank you for your help.


I tested this and Option 2 locks up the program in a few seconds of running. Why? Is it because CreateCompatibleBitmap uses up memory and is not automatically deleted when the function ends, as I have read that C++11 does automatically? Does it do something outside of the function's clean-up scope? Does that mean that I should avoid creating inside other code as in Option 2 because it is harder for the compiler to clean up at the end of a function? I think maybe. Further question: Where can I find documentation on this?





Last edited on
> I tested this and Option 2 locks up the program in a few seconds of running. Why?
Because you're leaking resources you don't free up by calling DeleteObject(hbmBuffer);

CreateCompatibleBitmap returns something that YOU need to keep a hold of and manage. C++ fanciness doesn't get you out of this hole without more effort.
It's no different to calling new, it's your responsibility to call delete.


1
2
3
4
5
6
7
8
9
10
11
class CCB {
  HBITMAP hbmBuffer;
public:
  CCB(hdc,right,bottom) { //!! you figure out the types
    hbmBuffer = CreateCompatibleBitmap(hdc, right, bottom);
  }
  ~CCB() {
    DeleteObject(hbmBuffer);
  }
  HBITMAP get() { return hbmBuffer; }
};

You might then be able to do
1
2
3
   CCB ccb(hdc, prc->right, prc->bottom);
   SelectObject(hdcWorkingBackBuffer, ccb.get());
   // This will clean up when ccb goes out of scope. 




> Option 2 better?
It's just wrong.

> Where can I find documentation on this?
MSDN
salem c

Thank you.

Got my answer.

Got a short, understandable, directly usable example of a class.

Topic archived. No new replies allowed.