gpt4 book ai didi

c++ - FreeImage delete[]位图数据失败

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:23 27 4
gpt4 key购买 nike

请帮助我在使用 FreeImage 加载位图后清理我的堆。不知何故删除[]数据;导致 _ASSERTE(_CrtIsValidHeapPointer(pUserData)) 断言,除了评论这一行之外,我找不到如何修复它。会不会有内存泄漏?任何帮助和解释将不胜感激!

pastebin 中的完整代码:http://pastebin.com/dWxz0tjM

Visual Studio 2012 解决方案(带有巨大的 FreeImage 静态库):http://rghost.ru/40322357 (15.7 兆字节!)

完整代码在这里:

#include <iostream>

// FreeImage static linkage
#define FREEIMAGE_LIB
#include "FreeImage/FreeImage.h"
#include "FreeImage/Utilities.h"
#pragma comment(lib, "FreeImage/FreeImaged.lib")

using namespace std;

static const wchar_t* sk_Filename = L"Test.tga";

// Error handler to use in callback
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *msg)
{
char buf[1024];
sprintf_s(buf, 1024, "Error: %s", FreeImage_GetFormatFromFIF(fif));

cout << buf;
}


// Bitmap loader from FreeImage samples
FIBITMAP* GenericLoaderU(const wchar_t* lpszPathName, int flag)
{
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;

fif = FreeImage_GetFileTypeU(lpszPathName, 0);
if(fif == FIF_UNKNOWN)
{
fif = FreeImage_GetFIFFromFilenameU(lpszPathName);
}

if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif))
{
FIBITMAP *dib = FreeImage_LoadU(fif, lpszPathName, flag);
return dib;
}
return NULL;
}

// Function gets filename and returns bitmap data array, its size and bits per pixel
void GetData(const wchar_t* szFilename, unsigned char* data, unsigned int& width, unsigned int& height, unsigned int& bpp)
{
FIBITMAP* src = GenericLoaderU(szFilename, 0);

if(src == 0)
return;

FIBITMAP* src32 = FreeImage_ConvertTo32Bits(src);
FreeImage_Unload(src);

// Get picture info
width = FreeImage_GetWidth(src32);
height = FreeImage_GetHeight(src32);
bpp = FreeImage_GetBPP(src32);
unsigned int scan_width = width * bpp/8;

if((width == 0) || (height == 0) || (bpp == 0))
return;


memset(data, 0, height * scan_width);
SwapRedBlue32(src32); // Convert BGR to RGB

// Get bitmap data
FreeImage_ConvertToRawBits(data, src32, scan_width, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);

FreeImage_Unload(src32);
return;
}

int main()
{
FreeImage_Initialise();
FreeImage_SetOutputMessage(FreeImageErrorHandler);

//Creating bitmap data array (size is unknown here)
unsigned char* data = new unsigned char[];
unsigned int width(0), height(0), bpp(0);

// Loading data here
GetData(sk_Filename, data, width, height, bpp);

//Using data here
cout << width << "x" << height << "x" << bpp << endl;
for (unsigned int i = 0; i < width * height * bpp/8; )
{
cout << "("
<< (unsigned int)data[i] << ", "
<< (unsigned int)data[i+1] << ", "
<< (unsigned int)data[i+2] << ", "
<< (unsigned int)data[i+3] << ")"
<< endl;

i += 4;
}
cout << endl;

//Cleanup
delete[] data; // <-- Breaks with _ASSERTE(_CrtIsValidHeapPointer(pUserData));
// What's wrong here?

system("pause");

return 0;
}

---编辑--------------------------------

好的,第一个可能的解决方案是使用 std::vector

最佳答案

delete无关

问题是 Debug Crt Runtime 只能在调用内存 API 时检查内存完整性,例如:malloc、free、realloc、new、delete。

Crt 检测到内存溢出。

显然,new unsigned char[] 没有为您分配足够的字节。

将分配移动到 GetData() 过程中并像这样调用它:

unsigned char* data = GetData(sk_Filename, width, height, bpp);

关于c++ - FreeImage delete[]位图数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12387794/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com