Access violation writing location 0x00000000

This is a discussion on Access violation writing location 0x00000000 within the Windows Programming forums, part of the Platform Specific Boards category; A class-based program of mine is giving some issues, in the form of unhandled exceptions. To summarize the program: The ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Access violation writing location 0x00000000

    A class-based program of mine is giving some issues, in the form of unhandled exceptions. To summarize the program:

    The main class, graphcalc, functions perfectly. It is created on the stack. It creates a seperate thread, which then creates a window. In the creation procedure for this window, an instance of the second class, drawbox, is created dynamically. It is not deleted anywhere.

    The problem occurs when trying to access member data/functions from member functions (other than the constructor). For example:

    Code:
    LRESULT CALLBACK drawbox::WndProc(HWND hwnd , UINT msg,WPARAM wParam , LPARAM lParam)
    {
    	hMain=hwnd; //access violation occurs here
    	switch (msg)
    	{
    	case WM_PAINT:
    		break;
    	default:
    		break;
    	}
    	return CallWindowProc (wpPrevProc , hwnd , msg , wParam , lParam);
    }
    This function is non-static, and is a member of drawbox. hMain is likewise non-static and a member of drawbox.

    As far as I know, this function SHOULD have the 'this' pointer, but it would appear it thinks it does, but actually doesn't.

    Any ideas as to whats going here?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Also, 'this' is NULL in the function.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Can you post your "thunking" code that calls drawbox::WndProc?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Is this what you mean?

    Code:
    LRESULT CALLBACK drawbox::WndProcS(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    	drawbox *lpDB;
    	if (msg==WM_NCCREATE)
    	{
    		LPCREATESTRUCT lpcs=(LPCREATESTRUCT)lParam;
    		lpDB=(drawbox *)lpcs->lpCreateParams;
    		SetWindowLong(hwnd,GWL_USERDATA,(LONG)lpDB);
    	}
    	lpDB=(drawbox *)GetWindowLong(hwnd,GWL_USERDATA);
    	return lpDB->WndProc(hwnd,msg,wParam,lParam);
    }
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yep. WM_NCCREATE is not the first message that your window procedure receives, so you are calling a function on a NULL class pointer. Just check that lpDB is not NULL before using it.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Won't I miss out on messages then?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    	if (!lpDB)
    		return 0;
    	return lpDB->StaticProc(hwnd,msg,wParam,lParam);
    This code prevents the window from being seen. The first message through the loop is WM_PAINT. Any ideas?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try:
    Code:
    if (!lpDB)
    	return DefWindowProc(hwnd, msg, wParam, lParam);
    else
    	return lpDB->StaticProc(hwnd, msg, wParam, lParam);
    >> Won't I miss out on messages then? <<

    Some obscure messages come through before NCCREATE but nothing you would want to handle(I think). However, you must pass them on to windows to process as above.

    >> The first message through the loop is WM_PAINT. <<

    I didn't think WM_PAINT came through before WM_NCCREATE but I could be wrong.

    EDIT: Of course. Missed that.
    Last edited by anonytmouse; 04-14-2004 at 03:54 AM.

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I just realized the problem. I'm subclassing the window, not superclassing it. Therefore I never receive a message that contains a pointer to the class instance (such as WM_CREATE or WM_NCCREATE). So, the whole time, I'm effectively calling the WndProc code without giving it a valid 'this' pointer. I'll superclass and see what happens. Thanks.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Works fine now.
    Code:
    if (!lpDB)
    	return DefWindowProc(hwnd, msg, wParam, lParam);
    else
    	return lpDB->StaticProc(hwnd, msg, wParam, lParam);
    Unfortunately it's not a default window, it's a static control. That would probably change things.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 09:56 PM
  2. Replies: 1
    Last Post: 08-13-2008, 02:51 AM
  3. "new" causes access violation
    By bennyandthejets in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:15 AM
  4. Strange access violation
    By jimmy_anttila in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2004, 03:10 AM
  5. Warning when building!
    By electrolove in forum C Programming
    Replies: 21
    Last Post: 02-12-2003, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21