Problem with CreateDIBSection

Posted on 2005-04-07
Last Modified: 2008-03-06
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.

Question by:PMH4514
    LVL 30

    Accepted 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 :-)

    Author Comment

    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?


    Author Comment

    hmm.. just saw it again :(

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now