Access Violation

Posted on 2004-09-07
Medium Priority
Last Modified: 2012-06-27
Even if my project is an OpenGL one, I'm pretty sure that my issue here is a C++ one. If I'm mistaken, I'll post the question in the appropriate section.

I'm currently building a OpenGL Engine that I could use in my futurs games. I read alot of code from nehe, and some books to try to analyse everything before starting the coding. Mainly, the basecode come from here : http://nehe.gamedev.net/counter.asp?file=files/basecode/nehegl_oop.zip

There's one part of the oop basecode that I didn't understood and the error seems to be comming from there :

LRESULT CALLBACK WndProc(      HWND      hWnd,
                                          UINT      uMsg,
                                          WPARAM      wParam,
                                          LPARAM      lParam)
      LONG user_data = GetWindowLong(hWnd, GWL_USERDATA);
      if (user_data == 0)
            if (uMsg == WM_CREATE)
                  CREATESTRUCT * creation = reinterpret_cast<CREATESTRUCT *>(lParam);
                  CHexEngine * appl = reinterpret_cast<CHexEngine *>(creation->lpCreateParams);
                  SetWindowLong(hWnd, GWL_USERDATA, reinterpret_cast<LONG>(appl));
                  appl->isVisible = true;
                  return 0;
            CHexEngine * appl = reinterpret_cast<CHexEngine *>(user_data);
            return appl->Message(hWnd, uMsg, wParam, lParam);
      return DefWindowProc(hWnd, uMsg, wParam, lParam);

The code compile and build. But when I'm trying to execute it, I get :
"First-chance exception in main.exe: 0xC0000005: Access Violation.
The program 'F:\Coding No Jutsu\_The Forest of Death\CHexEngine\Debug\main.exe' has exited with code 0 (0x0)."

If you want to check to code online :

If you want to download it

(Sorry for the lack of comments, license and credit ... these will be add shortly)

So how can I resolve this issue ?
Could you explain me, what is the code I posted doing exactly ?
Question by:WysG

Accepted Solution

Kitty__Kong earned 1200 total points
ID: 12002253
your problem is here

          if (uMsg == WM_CREATE)
               CREATESTRUCT * creation = reinterpret_cast<CREATESTRUCT *>(lParam);
               CHexEngine * appl = reinterpret_cast<CHexEngine *>(creation->lpCreateParams);
               SetWindowLong(hWnd, GWL_USERDATA, reinterpret_cast<LONG>(appl));
               appl->isVisible = true;
               return 0;

creation->lpCreateParams is NULL because you didnt set it to anything in the CreateWindow call. creation->lpCreateParams will be the value of lpParam from the CreateWindow call which is the last paramter to CreateWindow. the specific line that is causing the crash is

               appl->isVisible = true;

because appl is NULL

CreateWindow - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Windows/WindowReference/WindowFunctions/CreateWindow.asp
CREATESTRUCT - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Windows/WindowReference/WindowStructures/CREATESTRUCT.asp
LVL 39

Assisted Solution

itsmeandnobodyelse earned 800 total points
ID: 12003959
Kitty_Kong has found the error. I'll try to answer your questions:

>> So how can I resolve this issue ?

When calling CreateWindowEx() in CHexEngine::createWindow you have to do that:

    if (!(hWnd = CreateWindowEx(dwExStyle,
                                dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
                                0, 0,
                                WindowRect.right - WindowRect.left,
                                WindowRect.bottom - WindowRect.top,
                                (LPARAM)this)))   // pass the CHexEngine pointer to window

As you've passed NULL instead of 'this' you'll get a NULL pointer in WndProc what leads to the crash.

>> Could you explain me, what is the code I posted doing exactly ?

The callback function WndProc (or whatever name you gave her) will be called by the system (Windows) for ever Windows Message that has been directed to the window. You pass the function via function pointer when creating the window and the first message the window will get is WM_CREATE. You can see that that WndProc trusts on that:

>>>>    LONG user_data = GetWindowLong(hWnd, GWL_USERDATA);
The function tries to retrieve a long word that could be stored to a window using the GWL_USERDATA flag. Here, the pointer to CHexEngine should be stored.

>>>>     if (user_data == 0)
>>>>     {
>>>>          if (uMsg == WM_CREATE)
The very first call, user_data is 0 and the function now expects WM_CREATE message that is the first message the callback function will be call with.

>>>>  CREATESTRUCT * creation = reinterpret_cast<CREATESTRUCT *>(lParam);
>>>>  CHexEngine * appl = reinterpret_cast<CHexEngine *>(creation->lpCreateParams);

The LPARAM parameter of WM_CREATE message is a pointer to a CREATESTRUCT object that got filled with parameters passed with CreateWindowEx (and some others). What we need here is member 'lpCreateParams' that get filled with the lparam argument (last argument of CreateWindowEx).  That parameter is supposed to be a pointer to a CHexEngine object. That's why i passed 'this' in the code above.

>>>> appl->isVisible = true;

Here your prog crashes if NULL has been passed instead of 'this'.

>>>>          else
>>>>          {    
>>>>              CHexEngine * appl = reinterpret_cast<CHexEngine *>(user_data);
>>>>              return appl->Message(hWnd, uMsg, wParam, lParam);

'else' means that user_data is already filled, i. e. it's not the first message we got here. Then, user_data is a pointer to a CHexEngine and we pass the message to that class object by calling the member function Message(...)

Regards, Alex




Author Comment

ID: 12007683
Thanks Kitty__Kong for the fast anwser and thanks alot itsmeandnobodyelse for the explaination

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question