从IStream加载PNG时,GDI +崩溃

时间:2010-04-10 07:57:16

标签: c++ crash gdi+ png stream

我写了一些东西,通过GDI +从自定义C ++ IStream加载PNG文件。它运行得很好,直到我在Vista机器上运行它。每次都会崩溃。

在VS 2008上编译时,我发现在IStream::AddRef方法中插入代码(例如cout)会使问题消失。在使用VS 2010进行编译时,无论如何都会崩溃。

我把程序剥离了它的基础。我直接从Microsoft的文档中复制了一个FileStream。它可以在使用Bitmap::FromFile时加载PNG。它可以通过FromFileFromStream加载JPEG,GIF和BMP。

简而言之:在Vista上,通过Bitmap::FromStream崩溃加载PNG文件。

#pragma comment(lib, "gdiplus.lib")

#include <iostream>
#include <objidl.h>
#include <gdiplus.h>

class FileStream : public IStream
{
public: 
    FileStream(HANDLE hFile) 
    {
        _refcount = 1;
        _hFile = hFile;
    }

    ~FileStream()
    {
        if (_hFile != INVALID_HANDLE_VALUE)
        {
            ::CloseHandle(_hFile);
        }
    }

public:
    HRESULT static OpenFile(LPCWSTR pName, IStream ** ppStream, bool fWrite)
    {
        HANDLE hFile = ::CreateFileW(pName, fWrite ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ,
            NULL, fWrite ? CREATE_ALWAYS : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

        if (hFile == INVALID_HANDLE_VALUE)
            return HRESULT_FROM_WIN32(GetLastError());

        *ppStream = new FileStream(hFile);

        if(*ppStream == NULL)
            CloseHandle(hFile);

        return S_OK;
    }

    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject)
    { 
        if (iid == __uuidof(IUnknown)
            || iid == __uuidof(IStream)
            || iid == __uuidof(ISequentialStream))
        {
            *ppvObject = static_cast<IStream*>(this);
            AddRef();
            return S_OK;
        } else
            return E_NOINTERFACE; 
    }

    virtual ULONG STDMETHODCALLTYPE AddRef(void) 
    { 
        return (ULONG)InterlockedIncrement(&_refcount); 
    }

    virtual ULONG STDMETHODCALLTYPE Release(void) 
    {
        ULONG res = (ULONG) InterlockedDecrement(&_refcount);
        if (res == 0) 
            delete this;
        return res;
    }

    // ISequentialStream Interface
public:
    virtual HRESULT STDMETHODCALLTYPE Read(void* pv, ULONG cb, ULONG* pcbRead)
    {
  ULONG local_pcbRead;  
        BOOL rc = ReadFile(_hFile, pv, cb, &local_pcbRead, NULL);
  if (pcbRead) *pcbRead = local_pcbRead;
        return (rc) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
    }

    virtual HRESULT STDMETHODCALLTYPE Write(void const* pv, ULONG cb, ULONG* pcbWritten)
    {
        BOOL rc = WriteFile(_hFile, pv, cb, pcbWritten, NULL);
        return rc ? S_OK : HRESULT_FROM_WIN32(GetLastError());
    }

    // IStream Interface
public:
    virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER)
    { 
        return E_NOTIMPL;   
    }

    virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream*, ULARGE_INTEGER, ULARGE_INTEGER*,
        ULARGE_INTEGER*) 
    { 
        return E_NOTIMPL;   
    }

    virtual HRESULT STDMETHODCALLTYPE Commit(DWORD)                                      
    { 
        return E_NOTIMPL;   
    }

    virtual HRESULT STDMETHODCALLTYPE Revert(void)                                       
    { 
        return E_NOTIMPL;   
    }

    virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)              
    { 
        return E_NOTIMPL;   
    }

    virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)            
    { 
        return E_NOTIMPL;   
    }

    virtual HRESULT STDMETHODCALLTYPE Clone(IStream **)                                  
    { 
        return E_NOTIMPL;   
    }

    virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER liDistanceToMove, DWORD dwOrigin,
        ULARGE_INTEGER* lpNewFilePointer)
    { 
        DWORD dwMoveMethod;

        switch(dwOrigin)
        {
        case STREAM_SEEK_SET:
            dwMoveMethod = FILE_BEGIN;
            break;
        case STREAM_SEEK_CUR:
            dwMoveMethod = FILE_CURRENT;
            break;
        case STREAM_SEEK_END:
            dwMoveMethod = FILE_END;
            break;
        default:   
            return STG_E_INVALIDFUNCTION;
            break;
        }

        if (SetFilePointerEx(_hFile, liDistanceToMove, (PLARGE_INTEGER) lpNewFilePointer,
                             dwMoveMethod) == 0)
            return HRESULT_FROM_WIN32(GetLastError());
        return S_OK;
    }

    virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG* pStatstg, DWORD grfStatFlag) 
    {
        if (GetFileSizeEx(_hFile, (PLARGE_INTEGER) &pStatstg->cbSize) == 0)
            return HRESULT_FROM_WIN32(GetLastError());
        return S_OK;
    }

private:
    volatile HANDLE _hFile;
    volatile LONG _refcount;
};



#define USE_STREAM

int main()
{
 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
 ULONG_PTR gdiplusToken;
 Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

 Gdiplus::Bitmap *bmp;


#ifndef USE_STREAM
 bmp = Gdiplus::Bitmap::FromFile(L"test.png", false);
 if (!bmp)
 {
  std::cerr << " Unable to open image file." << std::endl;
  return 1;
 }
#else
 IStream *s;
 if (FileStream::OpenFile(L"test.png", &s, false) != S_OK)
 {
  std::cerr << "Unable to open image file." << std::endl;
  return 1;
 }

 bmp = Gdiplus::Bitmap::FromStream(s, false);
#endif

 std::cout << "Image is " << bmp->GetWidth() << " by " << bmp->GetHeight() << std::endl;

 Gdiplus::GdiplusShutdown(gdiplusToken);


#ifdef USE_STREAM
 s->Release();
#endif

 return 0;
}

跟踪和调试,表明它确实调用了一些IStream类。它在lastResult = DllExports::GdipCreateBitmapFromStream(stream, &bitmap);内部GdiPlusBitmap.h内崩溃,这是一个静态内联包装器,而不是平面API。

除引用计数外,它调用的唯一IStream方法是stat(文件大小),readseek

调用堆栈如下所示:

  • ntdll.dll!_DbgBreakPoint@0()+ 0x1 bytes
  • ntdll.dll!_RtlpBreakPointHeap@4()+ 0x28 bytes
  • ntdll.dll!_RtlpValidateHeapEntry@12()+ 0x70a3c bytes
  • ntdll.dll!_RtlDebugFreeHeap@12()+ 0x9a bytes
  • ntdll.dll!@ RtlpFreeHeap @ 16()+ 0x13cdd bytes
  • ntdll.dll!_RtlFreeHeap@12()+ 0x2e49 bytes
  • kernel32.dll!_HeapFree@12()+ 0x14 bytes
  • ole32.dll!CRetailMalloc_Free()+ 0x1c bytes
  • ole32.dll!_CoTaskMemFree@4()+ 0x13 bytes
  • GdiPlus.dll!GpPngDecoder :: GetImageInfo()+ 0x68 bytes
  • GdiPlus.dll!GpDecodedImage :: InternalGetImageInfo()+ 0x3c bytes
  • GdiPlus.dll!GpDecodedImage :: GetImageInfo()+ 0x18 bytes
  • GdiPlus.dll!CopyOnWriteBitmap :: CopyOnWriteBitmap()+ 0x49 bytes
  • GdiPlus.dll!CopyOnWriteBitmap :: Create()+ 0x1d bytes
  • GdiPlus.dll!GpBitmap :: GpBitmap()+ 0x2c bytes

我无法找到任何有同样问题的人,所以我认为我的实施有问题......

1 个答案:

答案 0 :(得分:1)

在Win7上没有给定代码和我自己的test.png的repro。我唯一看错的是你的Stat()函数,它没有完全初始化STATSTG。它在第一次调用时包含垃圾。

调用堆栈显示堆损坏。 Vista有一个新的,大大改进的堆管理器,可以比XP更快地诊断堆损坏。我只能假设腐败发生在未显示的代码中。