CreateWindowEx Access Violation

Posted in C/C++ LANGUAGE, PROGRAMMING by edeguzman on January 30, 2010

Have you ever experience loading DLL with a window class on single thread that went successful then upon unloading that DLL and you load that DLL for the second time but it crash? This article might be helpful to you.

This bug is kind a weird and really hard to trace. To make things worst MSDN some what provides a misleading documentation. I wasted 4 days working on this weird bug and ended up to have an easy fixed. I googled around but ended up to have a handful of information and found out that alot of people did encounter this kind of problem but no providing fixed.

Personally when I think of an access violation problem the only stuff that I thought of something related to pointers. It’s either you use a pointer without proper memory allocation and initialization. Or you use/access a pointer that was no longer on the heap. With these stuffs on my head I checked on every pointers that I had and found out they were all properly taken care of. And to make my pointers safe I even use smart pointers but still the problem exist.

I open the crashed dump and analyzed, I got the below output:
CRASH DUMP SCREENSHOT LINK

As you may see on the stack frames that the crashed occurred on the user32.dll which is one of the fundamental windows DLL. And it was on the call of CreateWindowEx that lead to crashed. I thought of I’m feeding the CreateWindowEx API with invalid data but I check every variables and guess what they were all valid. So I thought of that this was a f*cking windows API problem.

Then I found this URL http://blogs.msdn.com/oldnewthing/archive/2006/09/20/763727.aspx and this truly saves my day.

Solution to this problem :
1) Free resources used upon unloading the DLL.
2) For every window handle created make sure that it was destroyed upon exiting the DLL.
3) For every registered window class make sure that it was unregistered upon exiting the DLL.
4) For every child window class make sure that it was destroyed and unregistered before destroying the parent window class.

The number 4 was the one caused my problem. And that is why I say that MSDN was kind a providing a misleading document. If you read on the DestroyWindow() and Unregisterclass() on msdn documentation you might conclude that ; ok I’d DestroyWindow on the parent window I should no longer problem the associated child caused it was also destroy. But hey I should unregister the child window class caused it causes a memory leak on the heap but before unregistering the child window class I should destroy first all its created windows.

I might be doing an misinterpretation on the document. I think the MSDN tells us that upon exiting on the WindProc of parent window you should do atleast these :

DestroyWindow(ParentWindow);
Unregisterclass(“childClassWindow”,Instance);
Unregisterclass(“parentClassWindow”,Instance);

It would be ideally good if DestroyWindow also unregister the child class window.

References :

http://msdn.microsoft.com/en-us/library/ms644899(VS.85).aspx (DestroyWindow)
http://msdn.microsoft.com/en-us/library/ms632682(VS.85).aspx (Unregisterclass)
http://blogs.msdn.com/oldnewthing/archive/2006/09/20/763727.aspx

Advertisements

4 Responses

Subscribe to comments with RSS.

  1. robes said, on April 5, 2010 at 6:31 pm

    great article.

    robe blanche

  2. Debt Relief said, on April 23, 2010 at 3:25 pm

    Sorry for my bad english. Thank you so much for your good post. Your post helped me in my college assignment, If you can provide me more details please email me.

    • edeguzman said, on April 28, 2010 at 12:25 am

      hi mate, what exactly you want to know more on this area ?

  3. Julio Housen said, on May 23, 2011 at 5:16 am

    Great informativ post. Thank U for supporting details. Lookin’ ahead for ur next report. Cheers


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: