[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

Problem with CreateDIBSection

I have the following code which works 20-30 out of every 40-50 times I execute it:

      BITMAP bm = {0};
      BITMAPINFO* pbmInfo = new BITMAPINFO;
      ZeroMemory( &pbmInfo->bmiHeader, sizeof(BITMAPINFOHEADER) );
      pbmInfo->bmiHeader.biSizeImage = m_pData->size;
      pbmInfo->bmiHeader.biCompression = BI_RGB;

      void *buffer = NULL;
      HBITMAP hBmp = ::CreateDIBSection(0, pbmInfo, DIB_RGB_COLORS, &buffer, NULL, 0);

but at an apparrant random rate, my assertion fails, hBmp being null.  I immediately follow that with a GetLastError() but that returns 0, which, uh, indicates no error right? Any idea what the problem might be?

edit: I'll also note that m_pData->width and m_pData->Height are each 1000, bitdepth is 24 and size is 1000*1000*3 and even when it fails, m_pData and its associated members are all valid.

  • 2
1 Solution
Hi PMH4514,
>> ZeroMemory( &pbmInfo->bmiHeader, sizeof(BITMAPINFOHEADER) );
You should be zeroing out the entire struct,instead of just part of it.
Try the following:
ZeroMemory( pbmInfo, sizeof(*pbmInfo) );

David Maisonave :-)
PMH4514Author Commented:
Thanks David. I swapped in that code...


just created 200 images with it, no crash, I guess that did it.. Any explanations as to what the problem was?  Why not zeroing the entire struct would eventually cause a crash (as opposed to immediately?) some kind of memory overflow or something?

PMH4514Author Commented:
hmm.. just saw it again :(

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now