HOW can THIS cause an error?

Started by
43 comments, last by RunningInt 18 years, 5 months ago
You're probably not knowingly doing something with that variable, but your error sounds like it is likely caused by writing outside the bounds of an array, corrupting the stack, or some other invalid memory-related operation. Which will often cause problems somewhere after the actual error, not right at the error. In this case, I'm guessing the variable's location in memory is the reason why it consistently causes the actual failure of the program. Something else is corrupting stuff such that trying to use this variable turns into an attempt to read memory that you aren't allowed to read. Which means that the other code, the code that does the corrupting, is the important code. The actual line that crashes the program is just an innocent victim of the real error.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Advertisement
So then your code looks something like this?

int main(){   float yppf = -5.0f;   while(1)   {      if(yppf <= 0.0f && yppf >= -10.0f);      //no need for an exit condition since it crashes   }   return;}


*Edit*
Damn, too slow!
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
Quote:Original post by CHollman82
You don't need any more details,


The remark above quite clearly points out that you have no clue. You will need to give us more information before you can get an answer.

We do not know, among other things:
  • What language the code is in

  • What the address of the variable is, right before it crashes

  • What the exact error message is (that is, without useless [some adress in hex] remarks)

  • If the variable is a member, stack, global or static variable



The fourth point could be a very clear explanation of why the program crashes, yet you withhold that information from us, leaving us to hypothesize so many things about your code that it hardly becomes interesting to try and help you out. The second and third point combined could also help us check additional things.

Finally, running the program through a debugger and pointing out the exact line and ASM operation that causes the error to appear would give a great hint as to what is actually going on.
You probably have that variable linked to something. After a while you allocated too much memory or killed a vital process and it kills itself.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Removing that line may well make the program works and is a sure indication your bug is related to a memory buffer overrun or some other memory related problem, as the removal of that line just moves stuff around enoug in memory to hide the crash, the problem is NOT to do with that line.
As others have allready mentioned, we need more context info.

For example, here's a program with that line which would likely crash (well, in debug mode anyways, assuming the line dosn't get optimized out):

class foo {    float yppf;public:    void compare_stuff() {        if ( yppf <= 0.0f && yppf >= -10.0f );    }};int main () {    foo * example = 0xBAADF00D; //or some other invalid pointer    example->compare_stuff(); //foo attempts to access this->yppf, causing a crash    delete example;}


Here's another example, this time more hidden:

int main () {    int buffer[ 1 ];    foo * example = new foo;    for ( int i = 0 ; i < 10 ; ++i ) buffer = 0xBAADF00D; //buffer overflow    example->compare_stuff(); //may crash depending on if the buffer overflow overwrote the example pointer - if your compiler didn't but in some extra padding for it to check for buffer overflows    delete example;}


The problem fairly obviously does not stem from the comparison itself, but elsewhere (the buffer overflow) - even though the code will likely crash on the comparison line (again, assuming it's not optimized out). Written badly enough, just about anything can crash just about any other thing in a program.

Now if you're done telling us the problem is where it isn't (directly)... more details?
Sorry I tried to post the code where my blank post is but gdnet died as I was posting it. Here. As you can see I make it a const float (where as it WAS just a float) and comment out everywhere where it is used. AS IS the code will run for about 45 and then crash, without that line that I mentioned earlier it will run for as long as i've cared to test it.

<br>#include "main.h"<br><br>struct Marker<br>{<br> float x,y;<br>};<br><br>//Entry point<br>int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)<br>{<br> MSG msg;<br> float x = 504, y = SCR_HEIGHT-33; //in pixels<br> const float xppf = 0.0f, yppf = 0.0f; //in pixels per frame<br> //float xppf = 0.0f, yppf = 0.0f;<br> float xvel = 0, yvel = 0; //in pixels per second<br> float initxvel = 200.0f; //in pixels per second<br> float inityvel = 1000.0f;<br> float yPercentTransfer = 0.85f;<br> float xPercentTransfer = 0.85f;<br> float currentFPS = 0.0f, waitMult = 0.01f;<br> int frames = 0;<br> int markerIndex = 0;<br> int numMarkers = 0;<br> int gravity = 20;<br> bool rolling = false;<br> bool tracking = false;<br><br> Marker markers[1000];<br> <br> clock_t current;<br> //clock_t start;<br> //clock_t endTime;<br> <br><br>//*************************************<br>//** Initializaton **<br>//*************************************<br><br> //Seed the random number generator with the system time<br> current = time(NULL);<br> srand(current);<br><br> //Make a window and initialize DirectX in fullscreen mode<br> MakeWindow(hInstance);<br> InitD3D(SCR_WIDTH, SCR_HEIGHT, D3DFMT_A8R8G8B8, hwnd, false);<br><br> //Clear the screen<br> BeginDrawing();<br> EndDrawing();<br> Present();<br><br><br> //Setup DirectInput: keyboard<br> DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&lpdi, NULL);<br> lpdi-&gt;CreateDevice(GUID_SysKeyboard, &keyboard, NULL);<br> keyboard-&gt;SetDataFormat(&c_dfDIKeyboard);<br> keyboard-&gt;SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);<br> keyboard-&gt;Acquire();<br><br> //Create the textures<br> CTexture ball;<br> CTexture marker;<br> CTexture background;<br> <br> //Load the textures from the disk<br> ball.Init("gfx\\ball.png");<br> marker.Init("gfx\\marker.png");<br> background.Init("gfx\\background.jpg");<br><br><br><br> while (running)<br> {<br> //if(currentFPS!=0 && currentFPS&lt;60 && waitMult &gt; 0)waitMult-=.0001f;<br> //if(currentFPS&gt;65)waitMult+=.0001f;<br> <br> //endTime = clock() + waitMult * CLK_TCK;<br> <br> //while (clock() &lt; endTime) <br> //{<br> //Handle messages<br> if (PeekMessage (&msg,NULL,0,0,PM_REMOVE))<br> {<br> TranslateMessage (&msg);<br> DispatchMessage (&msg);<br> }<br> //}<br> <br> //INPUT<br> //Get Keystates<br> keyboard-&gt;GetDeviceState(sizeof(keystate),&keystate); <br> <br> //Escape exits<br> if(keystate[DIK_ESCAPE])<br> {<br> while(keystate[DIK_ESCAPE])keyboard-&gt;GetDeviceState(sizeof(keystate),&keystate);<br> running = false;<br> }<br> <br> if(keystate[DIK_F])<br> {<br> while(keystate[DIK_F])keyboard-&gt;GetDeviceState(sizeof(keystate),&keystate);<br> gravity = 0-gravity;<br> }<br><br> if(keystate[DIK_T])<br> {<br> while(keystate[DIK_T])keyboard-&gt;GetDeviceState(sizeof(keystate),&keystate);<br> if(!tracking)tracking=true;<br> else tracking=false;<br> }<br><br> if(keystate[DIK_ADD])<br> {<br> gravity+=2;<br> }<br><br> if(keystate[DIK_SUBTRACT])<br> {<br> gravity-=2;<br> }<br><br> if(keystate[DIK_RIGHT])<br> {<br> xvel+=2;<br> }<br><br> if(keystate[DIK_LEFT])<br> {<br> xvel-=2;<br> }<br> <br> if(keystate[DIK_SPACE])<br> {<br> if(xvel==0)xvel=initxvel;<br> yvel=inityvel;<br><br> //xppf = xvel/60.0f;<br> //yppf = yvel/60.0f;<br> }<br><br> //Move the ball<br> x+=xppf;<br> y+=0-yppf;<br> <br> if(yppf&lt;=0.0f && yppf&gt;=-10.0f);<br> <br> //Ball hit ground<br> if(y&gt;=SCR_HEIGHT-33)<br> {<br> y=SCR_HEIGHT-33;<br> yvel*=yPercentTransfer;<br> yvel=0-yvel;<br> <br> }<br><br> //Ball hit celieng<br> if(y&lt;=0)<br> {<br> y=0;<br> yvel*=yPercentTransfer;<br> yvel=0-yvel;<br> }<br><br> //Ball hit right side of screen<br> if(x&gt;SCR_WIDTH-33)<br> {<br> x=SCR_WIDTH-33;<br> xvel = 0-(xvel*xPercentTransfer);<br> }<br><br> //Ball hit left side of screen<br> if(x&lt;0)<br> {<br> x=0;<br> xvel = 0-(xvel*xPercentTransfer);<br> }<br><br> //Gravitational effect<br> yvel-=gravity;<br> <br> //Re-calculate pixel change per frame<br> //yppf = yvel/60.0f;<br> //xppf = xvel/60.0f;<br><br> <br> //DRAW THE SCENE<br> BeginDrawing();<br> <br> //background.Blit(0,0);<br> ClearBuffer();<br> <br> <br> if(tracking)<br> {<br> for(int i=0;i&lt;numMarkers;i++)<br> {<br> marker.Blit(markers<span style="font-weight:bold;">.x, markers<span style="font-weight:bold;">.y); <br> }<br> }<br><br> ball.Blit(x,y);<br> <br> <br> EndDrawing();<br> Present();<br> <br> //frames++;<br> //if(frames&gt;1)<br> //{<br> markers[markerIndex].x=(int)x+16;<br> markers[markerIndex].y=(int)y+16;<br> markerIndex++; <br> if(markerIndex&gt;1000) markerIndex=0; <br> if(numMarkers&lt;1000) numMarkers++;<br> frames=0;<br> //}<br><br> //current = clock();<br> //if(current-start &gt;= CLK_TCK){currentFPS = frames; frames=0; start=clock();}<br> <br> }<br><br>//*************************************<br>//** GAME OVER **<br>//*************************************<br><br> <br><br><br>//*************************************<br>//** SHUTDOWN **<br>//*************************************<br><br> //Cleanup all loaded textures<br> CTexture::CleanupTextures();<br><br> //Close DirectX<br> CloseD3D();<br><br> //Exit<br> return 0;<br>}<br><br><br><br><br><br>//Window procedure<br>LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT uMessage, WPARAM wParam, LPARAM lParam)<br>{<br> return DefWindowProc(hwnd,uMessage,wParam,lParam);<br>}<br><br><br>//Make window<br>int MakeWindow (HINSTANCE hInstance)<br>{<br> WNDCLASSEX wc;<br><br> wc.cbSize = sizeof(WNDCLASSEX);<br> wc.&#115;tyle = CS_HREDRAW | CS_VREDRAW;<br> wc.lpfnWndProc = WindowProcedure;<br> wc.cbClsExtra = 0;<br> wc.cbWndExtra = 0;<br> wc.hInstance = hInstance;<br> wc.hIcon = NULL;<br> wc.hCursor = NULL;<br> wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);<br> wc.lpszMenuName = NULL;<br> wc.lpszClassName = "Asteroids";<br> wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(5));<br><br> //Register class<br> if(!RegisterClassEx(&wc)) return false;<br><br> //Create window<br> hwnd = CreateWindowEx (NULL, "Asteroids", "Asteroids", NULL, 0, 0, SCR_WIDTH, SCR_HEIGHT, NULL, NULL, hInstance, NULL);<br> if(!hwnd) return false;<br><br> ShowCursor(false);<br> <br> //Success<br> return true;<br>}<br><br></code>
codeXtremeif(you->intelligence < radish->intelligence) you->STFU();
EDIT: Incorrect post removed

[Edited by - bakery2k1 on November 4, 2005 4:48:06 PM]
You have a buffer overrun. Markers is delcared to hold 1000 elements accessed 0 through 999. However you have:
if (numMarkers <
Beaten to it :)

This topic is closed to new replies.

Advertisement