-1

I am trying out a code to capture bitmap information using CreateDIBSection. Since I want to make the size of the array (which to hold each pixel value of the display monitor) flexible in order to suit monitor of different size, I create a dynamic unsigned char array (each color channel is 1 byte).

However, when I run the program, the program crashed right at the part of deleting the array, near the end of the program.

I tried to cast the array back to original type, i.e. unsigned char*, since I suspected it had been casted to void** when it being passed into CreateDIBSection(), but it doesn't work.

Below is the code, appreciate all the advice.

#include <Windows.h>
#include <cstdint>

HWND m_hwnd;

void GetBitMapInfo(const int& x_Coordinate, const int& y_Coordinate, const int& iWidth, const int& iHeight)
{
DWORD imageSize = iWidth * iHeight * 4; 

// Get the display window
HDC displayWindow = GetDC(m_hwnd);
HDC hdc = CreateCompatibleDC(displayWindow);

// Fill in the Bitmap information
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = iWidth;
bmpInfo.bmiHeader.biHeight = iHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = 0; 
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;

// Create the storage for the pixel information
uint8_t* image = new uint8_t[imageSize];

// Populate the storage with the BMP pixel information
HBITMAP hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, (void**)(&image), nullptr, NULL);

SelectObject(hdc, hBitmap);
BitBlt(hdc, x_Coordinate, y_Coordinate, iWidth, iHeight, displayWindow, 0, 0, SRCCOPY);

delete[] image; //Program crashed here: identifier "image" is undefined
image = nullptr;
DeleteDC(hdc);
DeleteDC(displayWindow);
DeleteObject(hBitmap);

return;
}

int main()
{
    GetBitMapInfo(0, 0, 1920, 1080);    
    return 0;
}
MK 5012
  • 29
  • 1
  • 9

1 Answers1

3

The CreateDIBSection function make the pointer

... receives a pointer to the location of the DIB bit values.

The memory you have allocated for image is lost (a memory leak) and the new pointer can't be passed to delete[] (that will lead to undefined behavior).

The documentation states that with a null pointer hSection the memory allocated by CreateDIBSection will be free'd by DeleteObject.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I tried to loop my program several times without deleting the dynamic array, and I realized the values stored inside the array varied, by right I should get back the same values since I am capturing the bitmap info for the same image. Thus, this makes me think that I must perform the delete to clear out any occupied memory. – MK 5012 Jan 30 '20 at 08:42
  • @John5012 That really depends on what `CreateDIBSection` is doing. Perhaps it allocates new memory internally that it give you? – Some programmer dude Jan 30 '20 at 08:43
  • @DanielLangr Missed that, thanks for pointing it out. Added to answer. – Some programmer dude Jan 30 '20 at 09:00
  • Some programmer dude, thanks. Now I understand that there is no need to allocate the memory for the image (**ppvBits), since if the hSection of CreateDIBSection is NULL, the system will allocate the memory, as stated in the documentation. – MK 5012 Jan 31 '20 at 09:33