Access Violation

Posted on 2004-09-07
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 :

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 300 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 -
LVL 39

Assisted Solution

itsmeandnobodyelse earned 200 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 -,
                                (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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now