locked
How to release a bitmap create by wicbitmap decoder?

    Question

  • My game is a music game.

    I need to provide a way that player can delete songs.

    All the file is deleted successfully except for a bitmap.

    Here is my code when loading a bitmap:

     
    	auto d2dFactory = mDeviceResources->GetD2DFactory();
    	d2dFactory->CreateDrawingStateBlock(&mStateBlock);
    	mDisplaySize.width = width;
    	mDisplaySize.height = height;
    	Microsoft::WRL::ComPtr<IWICBitmapDecoder> wicBitmapDecoder;
    	auto ImagingFactory = mDeviceResources->GetWicImagingFactory();
    	ImagingFactory->CreateDecoderFromFilename(fileName, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &wicBitmapDecoder);
    	Microsoft::WRL::ComPtr<IWICBitmapFrameDecode> wicBitmapFrame;
    	wicBitmapDecoder->GetFrame(0, &wicBitmapFrame);
    	Microsoft::WRL::ComPtr<IWICFormatConverter> wicFormatConverter;
    	ImagingFactory->CreateFormatConverter(&wicFormatConverter);
    	wicFormatConverter->Initialize(wicBitmapFrame.Get(), GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nullptr, 0.0, WICBitmapPaletteTypeCustom);
    	double DpiX = 96.0f, DpiY = 96.0f;
    	wicFormatConverter->GetResolution(&DpiX, &DpiY);
    	auto d2dContext = mDeviceResources->GetD2DDeviceContext();
    	d2dContext->CreateBitmapFromWicBitmap(wicFormatConverter.Get(), 
    		BitmapProperties(PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED), 
    		static_cast<float>(DpiX), static_cast<float>(DpiY)),
    		&mBitmap);
    	mBitmapSize = mBitmap->GetSize();
    	mIsResourceLoaded = true;

    And here is my distruction function to release bitmap resource:

    	mBitmap->Release();
    	mBitmap = nullptr;
    	mStateBlock->Release();
    	mStateBlock = nullptr;

    But the result is "access is denied".

    Then I try to delete this file manualy using explorer and it warn me that this file is open in my program.

    Please help me working out with this problem.

    Any help will be appreciated.

    Thanks.

    Tuesday, July 29, 2014 2:28 PM

Answers

  • Are you using smart pointer (ComPtr) on mBitmap? If you use ComPtr, you don't need to (and should not) manually release the reference. ComPtr takes care of it automatically when the object goes out of scope. So check if somehow your class has not gone out of scope. In addition, since it is a file, make sure you don't open it twice without first closing it. If you don't use ComPtr, then try to use it. Do not manually manage object lifetime, which can be error prone.
    • Marked as answer by Y.H.King Saturday, August 9, 2014 5:03 AM
    Tuesday, August 5, 2014 3:12 AM

All replies

  • If you have a areference to that file someone, that could be why you're having problems with deleting that file. If you run this whil also running process monitor, what does ProcMon tell you?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, August 1, 2014 7:46 PM
    Moderator
  • Are you using smart pointer (ComPtr) on mBitmap? If you use ComPtr, you don't need to (and should not) manually release the reference. ComPtr takes care of it automatically when the object goes out of scope. So check if somehow your class has not gone out of scope. In addition, since it is a file, make sure you don't open it twice without first closing it. If you don't use ComPtr, then try to use it. Do not manually manage object lifetime, which can be error prone.
    • Marked as answer by Y.H.King Saturday, August 9, 2014 5:03 AM
    Tuesday, August 5, 2014 3:12 AM