Sign in to follow this  
CHollman82

HOW can THIS cause an error?

Recommended Posts

if(yppf<=0.0f && yppf>=-10.0f); I do this in my main loop (it does nothing right now because I took everything out to debug it). My program runs for thousands of frames doing this comparison every frame and then will error out at roughly the same point each time (the point that the error occurs is within a few hundred frames each time). The error says: "The instruction at "[some address in hex]" referenced memory at "[some other address in hex]". The memory could not be "written". yppf is a float value. I have changed the code so that it remains the same (0) throughout and doesn't do anything and the program will still fail at roughly the same point. Commenting out this line will make the program run fine... WTF!

Share this post


Link to post
Share on other sites
Have you tried running the program in a debugger?

Anyway, you didn't exactly provide a lot of details to go on, so I doubt anyone can really help you. One line of code seems harmless enough, but what about the rest of your program? What are the hex addresses it reports in the error? what happens before/after this line?

Share this post


Link to post
Share on other sites
You don't need any more details, i said that I changed the code so that that variable is constant and NEVER referenced, other than in that if statement. I tried setting is as a constant so that both true and false would result from the if statement (not at the same time, lol). If you are seeing the ONLY point it is used in the program and you know that the program runs fine without that line of code, EVERYTHING ELSE BEING THE SAME, why do you need to know more about the program?

Share this post


Link to post
Share on other sites
Quote:
Original post by toucel
is the ; intentional? ;)


yes, and that is perfectly fine C++. I obviously had something there but removed it to debug it. Doing nothing, that comparison will create an error, even when the variable it is comparing is constant and never even used by the program, AND even after the program has run for ~45 seconds and that comparison has been evaluated thousands of times.

Share this post


Link to post
Share on other sites
Quote:
why do you need to know more about the program?
We need to know more about the program since that line does not look like it can cause an error, so the real cause is probably elsewhere.

Share this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
We need to know more about the program since that line does not look like it can cause an error, so the real cause is probably elsewhere.


Exactly, but the real case is not elsewhere, I do absolutely nothing with the variable being compared, and without that line the program runs fine.

Share this post


Link to post
Share on other sites
Statements in a program do not run in isolation. If your program is anything more than:


int main()
{
float yppf = 0x0f;

if(yppf<=0.0f && yppf>=-10.0f);

return 0;
}


Then the error is probably elsewhere.

Share this post


Link to post
Share on other sites
Quote:
Original post by CHollman82
You don't need any more details

Oh, that's nice. The person who *can't* find the error is telling people that they don't need more details to spot it? I suppose you know what the error is, and you're just testing us?

Oh, wait, or maybe you just shouldn't make assumptions when debugging.
I just tested your code. if your claims are true that the program does nothing else, and that yppf is a float with the constant value 0.0, then it runs perfectly, and has done so for a few billion iterations here now.

That leads me to the completely crazy guess that there must be something else in your program that interacts with it, making it crash.

Please, when you don't know what the problem is, and ask others for help, don't try to tell them that they don't need to see your program to fix the problem.

Quote:

If you are seeing the ONLY point it is used in the program and you know that the program runs fine without that line of code, EVERYTHING ELSE BEING THE SAME, why do you need to know more about the program?

Because based on the information you've given us, the program works perfectly.
Which means there's something important you haven't mentioned.

Edit:
For the record, here's the program I tested with:

int main(){
float yppf = 0.0;
while (true)
if(yppf<=0.0f && yppf>=-10.0f);
}


It has run for 5 minutes now, without crashing. Maybe I'm crazy, but this leads me to think that this line of code in isolation is not the problem.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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[i] = 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?

Share this post


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

[code lang="cpp"]
#include "main.h"

struct Marker
{
float x,y;
};

//Entry point
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
float x = 504, y = SCR_HEIGHT-33; //in pixels
const float xppf = 0.0f, yppf = 0.0f; //in pixels per frame
//float xppf = 0.0f, yppf = 0.0f;
float xvel = 0, yvel = 0; //in pixels per second
float initxvel = 200.0f; //in pixels per second
float inityvel = 1000.0f;
float yPercentTransfer = 0.85f;
float xPercentTransfer = 0.85f;
float currentFPS = 0.0f, waitMult = 0.01f;
int frames = 0;
int markerIndex = 0;
int numMarkers = 0;
int gravity = 20;
bool rolling = false;
bool tracking = false;

Marker markers[1000];

clock_t current;
//clock_t start;
//clock_t endTime;


//*************************************
//** Initializaton **
//*************************************

//Seed the random number generator with the system time
current = time(NULL);
srand(current);

//Make a window and initialize DirectX in fullscreen mode
MakeWindow(hInstance);
InitD3D(SCR_WIDTH, SCR_HEIGHT, D3DFMT_A8R8G8B8, hwnd, false);

//Clear the screen
BeginDrawing();
EndDrawing();
Present();


//Setup DirectInput: keyboard
DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&lpdi, NULL);
lpdi->CreateDevice(GUID_SysKeyboard, &keyboard, NULL);
keyboard->SetDataFormat(&c_dfDIKeyboard);
keyboard->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
keyboard->Acquire();

//Create the textures
CTexture ball;
CTexture marker;
CTexture background;

//Load the textures from the disk
ball.Init("gfx\\ball.png");
marker.Init("gfx\\marker.png");
background.Init("gfx\\background.jpg");



while (running)
{
//if(currentFPS!=0 && currentFPS<60 && waitMult > 0)waitMult-=.0001f;
//if(currentFPS>65)waitMult+=.0001f;

//endTime = clock() + waitMult * CLK_TCK;

//while (clock() < endTime)
//{
//Handle messages
if (PeekMessage (&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
//}

//INPUT
//Get Keystates
keyboard->GetDeviceState(sizeof(keystate),&keystate);

//Escape exits
if(keystate[DIK_ESCAPE])
{
while(keystate[DIK_ESCAPE])keyboard->GetDeviceState(sizeof(keystate),&keystate);
running = false;
}

if(keystate[DIK_F])
{
while(keystate[DIK_F])keyboard->GetDeviceState(sizeof(keystate),&keystate);
gravity = 0-gravity;
}

if(keystate[DIK_T])
{
while(keystate[DIK_T])keyboard->GetDeviceState(sizeof(keystate),&keystate);
if(!tracking)tracking=true;
else tracking=false;
}

if(keystate[DIK_ADD])
{
gravity+=2;
}

if(keystate[DIK_SUBTRACT])
{
gravity-=2;
}

if(keystate[DIK_RIGHT])
{
xvel+=2;
}

if(keystate[DIK_LEFT])
{
xvel-=2;
}

if(keystate[DIK_SPACE])
{
if(xvel==0)xvel=initxvel;
yvel=inityvel;

//xppf = xvel/60.0f;
//yppf = yvel/60.0f;
}

//Move the ball
x+=xppf;
y+=0-yppf;

if(yppf<=0.0f && yppf>=-10.0f);

//Ball hit ground
if(y>=SCR_HEIGHT-33)
{
y=SCR_HEIGHT-33;
yvel*=yPercentTransfer;
yvel=0-yvel;

}

//Ball hit celieng
if(y<=0)
{
y=0;
yvel*=yPercentTransfer;
yvel=0-yvel;
}

//Ball hit right side of screen
if(x>SCR_WIDTH-33)
{
x=SCR_WIDTH-33;
xvel = 0-(xvel*xPercentTransfer);
}

//Ball hit left side of screen
if(x<0)
{
x=0;
xvel = 0-(xvel*xPercentTransfer);
}

//Gravitational effect
yvel-=gravity;

//Re-calculate pixel change per frame
//yppf = yvel/60.0f;
//xppf = xvel/60.0f;


//DRAW THE SCENE
BeginDrawing();

//background.Blit(0,0);
ClearBuffer();


if(tracking)
{
for(int i=0;i<numMarkers;i++)
{
marker.Blit(markers[i].x, markers[i].y);
}
}

ball.Blit(x,y);


EndDrawing();
Present();

//frames++;
//if(frames>1)
//{
markers[markerIndex].x=(int)x+16;
markers[markerIndex].y=(int)y+16;
markerIndex++;
if(markerIndex>1000) markerIndex=0;
if(numMarkers<1000) numMarkers++;
frames=0;
//}

//current = clock();
//if(current-start >= CLK_TCK){currentFPS = frames; frames=0; start=clock();}

}

//*************************************
//** GAME OVER **
//*************************************




//*************************************
//** SHUTDOWN **
//*************************************

//Cleanup all loaded textures
CTexture::CleanupTextures();

//Close DirectX
CloseD3D();

//Exit
return 0;
}





//Window procedure
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd,uMessage,wParam,lParam);
}


//Make window
int MakeWindow (HINSTANCE hInstance)
{
WNDCLASSEX wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProcedure;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Asteroids";
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(5));

//Register class
if(!RegisterClassEx(&wc)) return false;

//Create window
hwnd = CreateWindowEx (NULL, "Asteroids", "Asteroids", NULL, 0, 0, SCR_WIDTH, SCR_HEIGHT, NULL, NULL, hInstance, NULL);
if(!hwnd) return false;

ShowCursor(false);

//Success
return true;
}

[/code]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You have a buffer overrun. Markers is delcared to hold 1000 elements accessed 0 through 999. However you have:
if (numMarkers <

Share this post


Link to post
Share on other sites

if(numMarkers<1000) numMarkers++;
assert(numMarkers<=1000);

for(int i=0;i<numMarkers;i++)
{
assert(i<numMarkers && numMarkers<=1000);
marker.Blit(markers[i].x, markers[i].y);
}


How does this read past the bounds? numMarkers is at most 1000, so i is at most 999.

However:


markers[markerIndex].x=(int)x+16;
markers[markerIndex].y=(int)y+16;
markerIndex++;
if(markerIndex>1000) markerIndex=0;


In this case, markerIndex can reach 1000 and cause an overflow.

Share this post


Link to post
Share on other sites
DOH, you are right about that. but that doesn't fix the error... Also, I have gotten past that point while the program was running without a problem, the error occurs a good 30 seconds after that point, and after fixing that the error still occurs.

Share this post


Link to post
Share on other sites
Quote:
Original post by CHollman82
Quote:
Original post by bakery2k1
We need to know more about the program since that line does not look like it can cause an error, so the real cause is probably elsewhere.


Exactly, but the real case is not elsewhere, I do absolutely nothing with the variable being compared, and without that line the program runs fine.
Someone responding like that DOES NOT want help, but simply wants to rant about their problem and blame the compiler.

Thankyou for calming down and allowing us to help you now.[smile]
We're working on it...

Share this post


Link to post
Share on other sites
Okay I've got nothing more than has already been mentioned so far. So Instead I have a question:

What happens if you comment out everything between BeginDrawing and EndDrawing? (besides not showing anything in the window any more)

Share this post


Link to post
Share on other sites
For large bodies of posted code, one should use sources tags (hit the edit button on this post for an example - although you won't be able to actually edit my post, you will get the form all the same which will show exactly how I do it).

The example:


int main () {
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this