• Advertisement

Archived

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

if () statement jumping to 0x00000000

This topic is 5712 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


Link to post
Share on other sites
Advertisement
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


Link to 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


Link to 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


Link to 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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites
You know you can just send the gmh without copying it:

SendData((char*) &gmh, sizeof(GAMEMSGHEADER));

Then you don''t have to worry about clobbering memory, which is probably what you were doing when you set:

transmit[sizeof(GAMEMSGHEADER)] = 1;

Share this post


Link to post
Share on other sites
// this statment is ILLEGAL and smashes the stack
transmit[sizeof(GAMEMSGHEADER)] = 0;

no matter what transmit''s size is, you should not depend
on it being the correct size ocmpared to yoru GAMESGHEADER
struct.

instead you should actually use your struct correctly
if it requires a "type" in it, then it should be part of
the struct. if you happen to change the GAMESGHEADER struct
for any reason you will break your code

if your smart you will modify your code to not rely on knowing the exact size of GAMESGHEADER when using sizeof(). VERY bad practice in this case (since you are using it for indexing into something).

also running the client and server on the same pc should work perfectly (with exception to udp if the server and client are supposed to listen on the same port which they should not). the only thing you lose is latency, and bandwidth restrictions associated with being on a network. though i agree it can sometimes be a bother if the game and server are fighting for the cpu.

Share this post


Link to post
Share on other sites

  • Advertisement