Need some help - SOLVED

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
SunShine
Posts: 31
Joined: February 16th, 2013, 2:44 pm

Need some help - SOLVED

Post by SunShine » August 24th, 2013, 10:29 pm

Hi everyone!

So I was coding "drawing bitmaps with Direct2D" from MSDN tutorials. Everything works fine, Initialization succedes, the bitmap draws , BUT the program throws an exception when I close it. It crashes when I am trying to release the bitmap ( m_bitmap->Release(); ) :(

Its the first time i got this kind of probplem - crashing when releasing objects/resources ...
Could you help me?
Last edited by SunShine on August 28th, 2013, 5:32 pm, edited 1 time in total.

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Need some HELP

Post by LuisR14 » August 25th, 2013, 2:01 am

having some code to look at would help ._. (also seems you accidently posted 2 threads :P)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Need some HELP

Post by LuX » August 25th, 2013, 7:13 am

Deleted the duplicate....

But yea, I get that sometimes too. Cannot remember what it was then, but I think it was because the object I'm trying to release was already released. So if you release an already released object it will crash. If you use ComPtr object it should release itself when it goes out of the scope.
ʕ •ᴥ•ʔ

User avatar
SunShine
Posts: 31
Joined: February 16th, 2013, 2:44 pm

Re: Need some HELP

Post by SunShine » August 25th, 2013, 10:06 am

Hi all.

The code is prety much the same as from MSDN, but here:

Code: Select all

bool BitmapClass::Initialize( LPCWSTR filename, ID2D1HwndRenderTarget* renderTarget )
{
        // NOTE: my bitmap is declared as   ID2D1Bitmap*   in the header

        IWICImagingFactory*       factory;
	IWICBitmapDecoder*        decoder;
	IWICBitmapFrameDecode* source;
	IWICFormatConverter*     converter;
	HRESULT result;


	// --- WIC FACTORY --- //
	result = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,            IID_IWICImagingFactory, (LPVOID*)&factory );
	if( FAILED( result ) )
		return false;

	// --- BITMAP DECODER --- //
	result = factory->CreateDecoderFromFilename( filename, NULL, GENERIC_READ,    WICDecodeMetadataCacheOnLoad, &decoder );
	if( FAILED( result ) )
		return false;

	result = decoder->GetFrame( 0, &source );
	if( FAILED( result ) )
		return false;

	// --- CONVERTER --- //
	result = factory->CreateFormatConverter( &converter );
	if( FAILED( result ) )
		return false;

	// Convert Bitmap pixel format
	result = converter->Initialize( source, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut );
	if( FAILED( result ) )
		return false;

	// Create ID2D1Bitmap
	result = renderTarget->CreateBitmapFromWicBitmap( converter, NULL, &m_bitmap );
	if( FAILED( result ) )
		return false;


	factory->Release();
	factory = NULL;

	converter->Release();
	converter = NULL;

	source->Release();
	source = NULL;

	decoder->Release();
	decoder = NULL;

	return true;
}

void BitmapClass::Shutdown()
{
        if( m_bitmap )
        {
	      m_bitmap->Release();  //  <<<<< CRASHES RIGHT HERE <<<<<<
	      m_bitmap = NULL;
        }

	return;
}
Theres really all in to it, if you want to see the MSDN thread:
http://msdn.microsoft.com/en-us/library ... s.85).aspx

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Need some HELP

Post by LuisR14 » August 25th, 2013, 6:55 pm

have you probably not initialize m_bitmap to null? probably the loading fails and since m_bitmap is not a valid pointer (but has an arbitrary value) it would fail o.o
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
SunShine
Posts: 31
Joined: February 16th, 2013, 2:44 pm

Re: Need some HELP

Post by SunShine » August 25th, 2013, 8:48 pm

Hi LuisR14.

I'm not exactly sure what you mean by that, but as I said beffore, the initialization succedes( the bitmap loads and i can draw it ). The only problem is when I try to release the bitmap( when i close the program )

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Need some HELP

Post by LuisR14 » August 26th, 2013, 12:16 am

oh ok, then probably is what Lux said :P
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

clau007
Posts: 15
Joined: July 30th, 2013, 8:36 pm
Location: Italy

Re: Need some HELP

Post by clau007 » August 27th, 2013, 9:17 am

Try to check if the pointer is not null before the release, like this:

Code: Select all

if(m_bitmap)
{
 m_bitmap->Release();
}

User avatar
SunShine
Posts: 31
Joined: February 16th, 2013, 2:44 pm

Re: Need some HELP

Post by SunShine » August 27th, 2013, 11:54 am

Hi clau007.

I did check, I allways do. ( see the code i wrote down, I think you missed that part ;) )

This is some kind of black magic :lol:
I hope someone knows the solution and will help :roll:

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Need some HELP

Post by chili » August 28th, 2013, 1:38 pm

Clean and zip your solution and post it here. Someone will run a debug on it and point you in the right direction.
Chili

Post Reply