删除动态数组时标识符未定义
Identifier is undefined when deleting a dynamic array
我正在尝试使用 CreateDIBSection 捕获位图信息的代码。由于我想使数组的大小(用于保存显示器的每个像素值)灵活以适应不同尺寸的显示器,因此我创建了一个动态无符号字符数组(每个颜色通道为 1 个字节)。
然而,当我运行程序时,程序在删除数组的部分崩溃了,接近程序的结尾。
我试图将数组转换回原始类型,即 unsigned char*,因为我怀疑它在传递给 CreateDIBSection() 时被转换为 void**,但它不起作用。
下面是代码,感谢大家的指教。
#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;
}
CreateDIBSection
函数使指针
... receives a pointer to the location of the DIB bit values.
您为 image
分配的内存丢失(内存泄漏)并且无法将新指针传递给 delete[]
(这将导致 未定义的行为).
文档指出,使用空指针 hSection
,CreateDIBSection
分配的内存将由 DeleteObject
释放。
我正在尝试使用 CreateDIBSection 捕获位图信息的代码。由于我想使数组的大小(用于保存显示器的每个像素值)灵活以适应不同尺寸的显示器,因此我创建了一个动态无符号字符数组(每个颜色通道为 1 个字节)。
然而,当我运行程序时,程序在删除数组的部分崩溃了,接近程序的结尾。
我试图将数组转换回原始类型,即 unsigned char*,因为我怀疑它在传递给 CreateDIBSection() 时被转换为 void**,但它不起作用。
下面是代码,感谢大家的指教。
#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;
}
CreateDIBSection
函数使指针
... receives a pointer to the location of the DIB bit values.
您为 image
分配的内存丢失(内存泄漏)并且无法将新指针传递给 delete[]
(这将导致 未定义的行为).
文档指出,使用空指针 hSection
,CreateDIBSection
分配的内存将由 DeleteObject
释放。