• Advertisement

• Popular Now

• 11
• 9
• 10
• 9
• 11
• Advertisement
• Advertisement
• Advertisement

Archived

This topic is now archived and is closed to further replies.

if () statement jumping to 0x00000000

This topic is 5804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve got a WndProc function which has the standard switch () statement for parsing the WM_* messages. One of my case labels is a WM_KEYDOWN message. I''ve tried two methods to parse what key has been pressed. I first used a switch statement with VK_UP, VK_DOWN, VK_LEFT, and VK_RIGHT and afterwards I tried four if statements with the same keycodes. However, whenever the program got to the if or switch statement, the application jumped to 0x00000000 and caused an access violation. Any ideas as to why this could be? Any help would be much appreciated.

Share this post

Share on other sites
Advertisement
No ideas. Post some code, then I''m sure it''ll be easier to diagnose.

Share this post

Share on other sites
Here''s the code. The program is a client/server for a game. Basically what the code should do is if the program is running as a client and a key is pressed, then data is sent back to the server about what key was pressed.

  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){	switch (message) 	{		case WM_KEYDOWN:			char transmit[4];			GAMEMSGHEADER gmh;			int keycode;			memset(transmit, 0, 4);			gmh.ID = ''D'';			gmh.usSize = 1;			memcpy(transmit, &gmh, sizeof(GAMEMSGHEADER));			keycode = (int)wParam;			if (bClient) {				if (keycode == VK_UP) {					transmit[sizeof(GAMEMSGHEADER)] = 0;					Cli_SendData(transmit, 4);				}				if (keycode == VK_RIGHT) {					transmit[sizeof(GAMEMSGHEADER)] = 1;					Cli_SendData(transmit, 4);				}				if (keycode == VK_DOWN) {					transmit[sizeof(GAMEMSGHEADER)] = 2;					Cli_SendData(transmit, 4);				}				if (keycode == VK_LEFT) {					transmit[sizeof(GAMEMSGHEADER)] = 3;					Cli_SendData(transmit, 4);				}			}			break;

There are a few more WM_* messages after this, but the error occurs during the...
if (keycode == VK_UP) {
...line of code. The dissassembly doesn''t reveal much either. As far as I can tell, the "jne" command is jumping to the correct location, but the program just jumps out to 0x00000000, the call stack is emptied and an access violation ensues.

Share this post

Share on other sites
Are you smashing the stack? Is sizeof(GAMEMSGHEADER) 4 or less? Munging the stack can lead to all sorts of odd errors.

Just Plain Wrong

Share this post

Share on other sites
I think it''s started working...it''s amazing what a simple ''restart-the-computer'' can do...

Share this post

Share on other sites
What is the size of GAMEMSGHEADER?

Share this post

Share on other sites
quote:
Original post by Mayrel
Are you smashing the stack? Is sizeof(GAMEMSGHEADER) 4 or less? Munging the stack can lead to all sorts of odd errors.

Ah, you got it first . You mean 3 or less, but it''s just a typo.

Share this post

Share on other sites
Yeah, sizeof(GAMEMSGHEADER) is 3 (well it was 4, before i put in "#pragma pack(1)", as with the size it 4 it was causing more errors...). Nevertheless, the if statement is working now.

Thanks anyway.

Share this post

Share on other sites
To go on a tangent: You should put a set of braces are the case statement if you''re declaring variables in it. For example:
switch(asdf) {  case ALPHA: {      int a;    }    break;}

Otherwise the compiler can (rightfully) be confused about whether or not to declare something.

Share this post

Share on other sites
Hmm, yeah. I hate writing Network programs using only one computer. Trying to run a client game and a server game on one computer is just asking for trouble.

Share this post

Share on other sites

• Advertisement