tags:

views:

247

answers:

2

Heyo,
My program, which does exactly the same thing every time it runs (moves a point sprite into the distance) will randomly fail with the text on the terminal 'Illegal Instruction'. My googling has found people encountering this when writing assembly which makes sense because assembly throws those kinds of errors.

But why would g++ be generating an illegal instruction like this? It's not like I'm compiling for Windows then running on Linux (which even then, as long as both are on x86 shouldn't AFAIK cause an Illegal Instruction). I'll post the main file below.

I can't reliably reproduce the error. Although, if I make random changes (add a space here, change a constant there) that force a recompile I can get a binary which will fail with Illegal Instruction every time it is run, until I try setting a break point, which makes the illegal instruction 'dissapear'. :(

#include <stdio.h>
#include <stdlib.h> 
#include <GL/gl.h>
#include <GL/glu.h>

#include <SDL/SDL.h>

#include "Screen.h"  //Simple SDL wrapper
#include "Textures.h" //Simple OpenGL texture wrapper 
#include "PointSprites.h" //Simple point sprites wrapper


double counter = 0;
/* Here goes our drawing code */
int drawGLScene()
{
    /* These are to calculate our fps */
    static GLint T0     = 0;
    static GLint Frames = 0;

    /* Move Left 1.5 Units And Into The Screen 6.0 */
    glLoadIdentity();
 glTranslatef(0.0f, 0.0f, -6);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

 glEnable(GL_POINT_SPRITE_ARB);
 glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
    glBegin( GL_POINTS );   /* Drawing Using Triangles */
 glVertex3d(0.0,0.0, 0);
 glVertex3d(1.0,0.0, 0);
 glVertex3d(1.0,1.0, counter);
 glVertex3d(0.0,1.0, 0);
    glEnd( );                           /* Finished Drawing The Triangle */

    /* Move Right 3 Units */


    /* Draw it to the screen */

    SDL_GL_SwapBuffers( );
    /* Gather our frames per second */
    Frames++;
    {
 GLint t = SDL_GetTicks();
 if (t - T0 >= 50) {
     GLfloat seconds = (t - T0) / 1000.0;
     GLfloat fps = Frames / seconds;
     printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
     T0 = t;
     Frames = 0;
  counter -= .1;
 }
    }

    return 1;
}

GLuint objectID;
int main( int argc, char **argv )
{
 Screen screen;
 screen.init();
 screen.resize(800,600);


 LoadBMP("./dist/Debug/GNU-Linux-x86/particle.bmp");
 InitPointSprites();


 while(true){drawGLScene();} 
}
+6  A: 

The compiler isn't generating illegal exceptions, with 99.99% probability. Almost certainly what's happening is that you have a bug in your program which is causing it to either a) overwrite parts of your executable code with garbage data, or b) use a function pointer that points into garbage. Try running your program under valgrind to diagnose the problem - http://valgrind.org/.

JSBangs
A variant of (b) to watch out for in C++ is calling a virtual function on an object that has been overwritten somehow.
Mike Dinsdale
A: 

It's probably the following line:

GLfloat fps = Frames / seconds;
Sam Hocevar
How could this overwrite executable code?
That might give an arithmetic exception, but not an illegal instruction.
Mike Seymour
@Sam If you're worried about a divide-by-zero error, that would cause an arithmetic exception (SIGFPE), not an illegal instruction (SIGILL)
Michael Mrozek
It wouldn't. I strongly doubt that this is the source of the problem.
JSBangs