1

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

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

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

完整代码在这里:

#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.

4

3 回答 3

1

它与 . 无关delete

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

Crt 检测到内存溢出。

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

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

unsigned char* data = GetData(sk_Filename, width, height, bpp);
于 2012-11-08T22:40:00.360 回答
0

编写一个函数,根据图像计算数据的大小。

然后分配该大小的数据

例如

size_z GetDataSize(const wchar_t* szFilename)
于 2012-09-12T12:35:48.937 回答
0

在函数内部计算所需的大小很容易GetData,因此在此处分配数组并返回它。

你将会拥有

unsigned char* GetData(const wchar_t* szFilename, 
                       unsigned int& width, 
                       unsigned int& height, 
                       unsigned int& bpp);

其中包含

unsigned char* data = new unsigned char[height * scan_width];
// Do the conversion...
return data;

并且main会说

unsigned char* data = GetData(sk_Filename, width, height, bpp);
于 2012-09-12T13:52:51.090 回答