使用delete []删除指针时崩溃

时间:2015-12-06 09:13:16

标签: c++ visual-c++

我用来输出pTemp的代码:

if (m_pTImgFrame != NULL)
{
    for (int i = 0; i < m_nFrameNumber; i++)
    {
        TImage* pTemp = (TImage*)m_pTImgFrame[i];
        if (pTemp != NULL)
        {
            TCHAR buffer[256] = { 0 };
            _stprintf_s(buffer, 256, _T("%d,%d,%d,%d,%d,%d,%d,%d\n"), 
                pTemp->attrib,
                pTemp->h,
                pTemp->pitch,
                pTemp->w,
                pTemp->data[0],
                pTemp->data[1], 
                pTemp->data[2], 
                pTemp->data[3]);
            OutputDebugString(buffer);

            delete[] pTemp;
            m_pTImgFrame[i] = NULL;
        }
    }
    delete []m_pTImgFrame;
    m_pTImgFrame = NULL;
}

来源代码仍然崩溃

if (m_pTImgFrame != NULL)
{
    for (int i = 0; i < m_nFrameNumber; i++)
    {
        BYTE* pTemp = (BYTE*)m_pTImgFrame[i];
        if (pTemp != NULL)
        {
            delete[] pTemp;
            m_pTImgFrame[i] = NULL;
        }
    }
    delete []m_pTImgFrame;
    m_pTImgFrame = NULL;
}

变量声明:

typedef unsigned int        UINT;
typedef struct _TImage
{
    int  w;         
    int  h;         
    int  pitch;     
    int  attrib;    
    unsigned char data[4];  
} TImage;

UINT        m_nFrameNumber;     
UINT*       m_pnFrameDelay;     
TImage**    m_pTImgFrame;

PBITMAPINFOHEADER pbih; 
typedef struct tagBITMAPINFOHEADER{
    DWORD      biSize;
    LONG       biWidth;
    LONG       biHeight;
    WORD       biPlanes;
    WORD       biBitCount;
    DWORD      biCompression;
    DWORD      biSizeImage;
    LONG       biXPelsPerMeter;
    LONG       biYPelsPerMeter;
    DWORD      biClrUsed;
    DWORD      biClrImportant;
} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;

分配:

    m_pTImgFrame = new TImage*[1];
    m_pTImgFrame[0] = (TImage*)new BYTE[sizeof(TImage) - 4 + 4 * width * pbih->biHeight];
    m_pTImgFrame[0]->attrib = imageAttrib8888;
    m_pTImgFrame[0]->w = width;
    m_pTImgFrame[0]->h = pbih->biHeight;
    m_pTImgFrame[0]->pitch = 4 * width;
    LOG(_T("32width=%d, height=%d"), width, pbih->biHeight);
    for(j=pbih->biHeight-1; j>=0; j--)
    {
        int indexDst = (pbih->biHeight-j-1) * width * 4;
        //int indexSrc = j * pbih->biWidth * 4;
        int indexSrc = j * ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8/*bmpInfo.bmWidthBytes*/;
        for(i=0; i< width; i++)
        {
            m_pTImgFrame[0]->data[indexDst++] = lpBits[indexSrc++];
            m_pTImgFrame[0]->data[indexDst++] = lpBits[indexSrc++];
            m_pTImgFrame[0]->data[indexDst++] = lpBits[indexSrc++];
            m_pTImgFrame[0]->data[indexDst++] = 255;
            indexSrc++;
        }
    }

运行该行时代码将崩溃 delete [] pTemp如果m_nFrameNumber为1(我只看到崩溃发生时为1但我不确定); pTemp的每个值的输出都是正确的,那么它为什么会崩溃?

1 个答案:

答案 0 :(得分:0)

如果delete [] pTemp m_nFrameNumber ,则在运行第1行时代码会崩溃

鉴于你刚才说的话,分配如下:

m_pTImgFrame = new TImage*[1];

m_pTImgFrame唯一有效的索引是0,因为您只将数组分配为1大小。尝试访问m_pTImgFrame[1]是未定义的行为。

其次,您的新运算符需要匹配类型和标量的删除运算符。

您已分配为字节数组:

m_pTImgFrame[0] = (TImage*)new BYTE[sizeof(TImage) - 4 + 4 * width * pbih->biHeight];

因此解除分配元素的正确方法如下:

BYTE* allocation = (BYTE*)m_pTImgFrame[0];
delete [] allocaiton;

这是错误的:

    TImage* pTemp = (TImage*)m_pTImgFrame[i];
    if (pTemp != NULL)
    {
        ...
        delete[] pTemp; // deleting as an array of TImage, when it should be deleted as an array of bytes
    }

基于yoru代码的无关切线 - 在调用delete之前没有必要检查NULL。说delete NULL总是安全的。永远都是。