What do you do when you have no idea why something doesn't work?

Started by
6 comments, last by BradDaBug 22 years, 6 months ago
i''m working on this game, and i''m trying to encapsulate Direct3D and OpenGL into this class so i can use either one to draw the graphics using the same function calls. anyway, WinMain is giving me fits. The game compiles and runs when I comment out this funtion that loads certain settings from a file, but when I put the function back, the game crashes at strange places. One place is where I copy the hwnd of the window into another global hwnd. Heres the code: winHInstance = hinstance; winHInstance is a global. hinstance is from the WinMain parameter list thing. So I comment that out. Further down, it crashes here: winclass.cbSize = sizeof(WNDCLASSEX); That code''s from where it fills in the Window Class. The LoadSettings() function has NOTHING to do with Windows, and is just standard C code! WHAT IS GOIN ON!?
I like the DARK layout!
Advertisement
You''ve probably encountered a "typo-bug". In any case, noone can really help you unless you post some of the code around the statement.

You''ll probably find the solution on your own.
If its not a long code, sometimes its good to just write it over.

"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden
------------------------------Put THAT in your smoke and pipe it
HO! Whats this?!

OK, I''ve snipped out a piece of code that when uncommented, everything works great. Heres the little booger...

else if (strncmp(buffer, "SCREEN RES: ",12) == 0)
{
WriteLog("Parsing resolution...");
strcpy(secondbuffer,strchr(buffer, 58)+2);
WriteLog("Resolution: %s",secondbuffer);

sscanf(secondbuffer,"%n, %n, %n",&GfxWidth,&GfxHeight,&GfxDepth);
}

This is in a bigger if...then block. Each case represents whatever label the line has. This one has "SCREEN RES:" since nearly each label has a different data format, each one is handled seperatly in its own case.

"buffer" is a pointer to a string holding the current line its working with.

"secondbuffer" is another pointer to a string that the junk following the ":" goes into (the important part, in other words)

The line this piece of code is working with looks something like this: "SCREEN RES: 320, 200, 16" (minus the quotes)

It doesn''t do anything with these numbers except load them into variables. I could put -420, 32000, 1000 and it wouldn''t make any difference.

Remmember...this code doesn''t crash, but it seems to be causing other code to crash. Why?

I like the DARK layout!
nuts...make that when commented out it works great.
I like the DARK layout!
Sometimes a line isn''t properly terminated somewhere (and sometimes it''s actually legal!) Commenting out that segment might allow such a line to affect the parsing of the rest of the file. Then again, maybe not. Go over your source and look for proper line termination. Also remember that an ''else'' will be parsed with the closest preceding ''if'' statement unless nested in curly brackets.
Awesome! I fixed it!

it turned out the "secondbuffer" wasn''t big enough to hold all those numbers. It took 12 characters to hold that string, and it was only 10 characters long.

I don''t get why it crashed it so far later down the road, but at least I fixed it!

Thanks a bunch for yall''s help. Most of the time I can eventually fix whatever''s going on by talking through it like this. It also helps me to stop pulling my hair out.
I like the DARK layout!
Because of how memory protection works, it''s usually OK to overwrite a buffer by a few bytes. That is, until something else tries to use those few bytes, and they''ve been changed by your incorrect code...

If you must use static buffers for reading files (or anything, for that matter), I always like to make them 3 or 4 times bigger than I''d ever expect to need. In your case, make secondBuffer something like 128 (I also like to make them powers of two ). 128 bytes is not much, and it''ll be reclaimed when your function goes out of scope anyway.

codeka.com - Just click it.

This topic is closed to new replies.

Advertisement