PDA

View Full Version : Access Violation using SOIL (only when adding an extra source file)



felixrulz
08-12-2012, 08:39 PM
Hey,

Here is the error message I am recieving from VS2010:

Unhandled exception at 0x77358dc9 in AzimuthalEqui.exe: 0xC0000005: Access violation writing location 0x00000014.

The line of code generating this error is:

texture[1] = SOIL_load_OGL_texture
(
texture_filename,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID, // create ID automatically
SOIL_flags
);

before entering this function:


texture_filename = "Data/texture.jpg"
SOIL_LOAD_AUTO (= 0)
SOIL_CREATE_NEW_ID (=0)
SOIL_flags = 16 (=SOIL_FLAG_INVERT_Y)

The problem is that when I include a file I have written to parse some information in a text file an error is generated, otherwise the texture loads and is displayed. The prob is most likely caused by one function as when this particular function is removed the code loads and displays the texture.
Here is the function that when added to my project makes the error occur:

void get_user_points(double *lats, double *longs){

char buffer[BUFFSIZE_PARSE];
char *p_buff = buffer;
FILE *fp;
const char *filename = "Points.txt";
double temp;
double *tmp_p = &temp;


fp = fopen(filename,"r+");
if (fp == NULL)
{
sprintf(buffer, "Can't Find File: %s", filename);
MessageBoxA(NULL, buffer, "ERROR", MB_OK|MB_ICONEXCLAMATION);
exit(0);
}

fgets(buffer, BUFFSIZE_PARSE, fp);

while (*(p_buff+1) != '\0'){

p_buff = get_next_letter(p_buff);

switch (tolower(*p_buff)){
case 'n':
putchar(*p_buff);
p_buff++;
p_buff=get_next_double(lats, p_buff);
printf(" = %f\n", *lats);
break;
case 's':
putchar(*p_buff);
p_buff++;
p_buff=get_next_double(lats, p_buff);
printf(" = %f\n", *lats);
break;
case 'e':
putchar(*p_buff);
p_buff++;
p_buff=get_next_double(longs, p_buff);
printf(" = %f\n", *longs);
break;
case 'w':
putchar(*p_buff);
p_buff++;
p_buff=get_next_double(longs, p_buff);
printf(" = %f\n", *longs);
break;
case 'r':
putchar(*p_buff);
p_buff++;
p_buff=get_next_double(tmp_p, p_buff);
printf(" = %f\n", *tmp_p);
break;
case 'g':
putchar(*p_buff);
p_buff++;
p_buff=get_next_double(tmp_p, p_buff);
printf(" = %f\n", *tmp_p);
break;
case 'b':
putchar(*p_buff);
p_buff++;
p_buff=get_next_double(tmp_p, p_buff);
printf(" = %f\n", *tmp_p);
break;

default:
break;
}
}

putchar('\n');


fclose(fp);

}


Here is my reduced code (but i don't think this is the issue). [There may be some remanants of my full code]:

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

{
printf("Initialising...\n");
glutInitWindowSize(700, 700);
glutInitWindowPosition(0, 0);
glutCreateWindow ("SOIL Texture Test");
}

InitGL();
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);

glutMainLoop();

return 0;
}

int InitGL(GLvoid) // Setup OpenGL
{
//Load textures
if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
MessageBox(NULL,TEXT("Cannot Load Image for Texture Map"),TEXT("Error!"),MB_OK | MB_ICONINFORMATION);
//return false; // If Texture Didn't Load Return FALSE ( NEW )
}
else
glEnable(GL_TEXTURE_2D); // Enable texture mapping


glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set the background colour (to black)

glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // Select testing type


//get_user_points(&user_lat[0], &user_long[0]);

return true; // Initialization went OK
}


void DrawGLScene(GLvoid) // OpenGL drawing function
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the Screen and the Depth Buffer

glLoadIdentity();
glTranslatef(0.0f,0.0f,z);

glBindTexture (GL_TEXTURE_2D, texture[filter]);
glBegin (GL_QUADS);
glNormal3f(0, 0, 1);

glTexCoord2f (0,0);
glVertex3f (-3,-3 ,0);
glTexCoord2f (1,0 );
glVertex3f (3,-3 , 0);
glTexCoord2f (1, 1);
glVertex3f (3,3 , 0);
glTexCoord2f (0,1 );
glVertex3f (-3,3 ,0 );
glEnd();

glFlush();
}

void ReSizeGLScene(int w, int h) // Code to resize (and initialise) the OpenGL scene (run once when in fullscreen mode)
{
// Set up perspective view matrix
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)w/(GLfloat)h, 0.1f, 100.0f);
//glOrtho(-50.0, 50.0, -50.0, 50.0, -50.0, 50.0);

// Set up modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int LoadGLTextures(void) // Load Bitmaps And Convert To Textures
{
unsigned int SOIL_flags;
GLint mag_param;
GLint min_param;

printf("Loading Textures... ");

SOIL_flags = SOIL_FLAG_INVERT_Y;
mag_param = GL_NEAREST;
min_param = GL_NEAREST;


texture[1] = SOIL_load_OGL_texture
(
texture_filename,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID, // create ID automatically
SOIL_flags
);

if(texture[1] == 0)
return false;

glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,mag_param);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,min_param);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

printf("Textures Loaded\n");
return true;
}


Hopfully I have explained the problem well.
Thanks.


EDIT:

I believe I have narrowed the problem down. It has something to do with opening the file for reading... if I comment out these lines the texture loads correctly:


//fpr = fopen(filename2,"rb");
...
...
...
//fclose(fpr);


I'm still not sure how this could affect SOIL when I am not even calling the function. I have tried renaming the file pointer but there is no effect.

Thanks.

V-man
08-13-2012, 01:13 PM
Have you inspected the contents of texture_filename? Does it look like C:\folder\myfilename.xxx?
You might want to study on how to use the debugger that comes with your compiler.
Compile a debug version of SOIL and step into the code to see where it crashes.

felixrulz
08-13-2012, 05:34 PM
Have you inspected the contents of texture_filename? Does it look like C:\folder\myfilename.xxx?


yes, the variable texture_filename is "Data/texture.jpg". (see start of previous post). It dosn't have "C:/..." but this dosn't seem to be a problem as it loads fine before integrating my new function.


You might want to study on how to use the debugger that comes with your compiler.
I'm not too sure what you mean by this. Currently I use watches, break points, line stepping, etc. Are the other useful tools I don't know about?


Compile a debug version of SOIL and step into the code to see where it crashes.
Sounds like a good idea. I'll try it soon(ish).

Cheers.

felixrulz
08-13-2012, 08:27 PM
Sounds like a good idea. I'll try it soon(ish).


I haven't actualy compliled my own .lib file yet, but includeing the .c files in my project fixed the problem. I will try compiling the library now.


EDIT: Yep, it seems the library I downloaded was corrupted or something. After building own, works
Thanks V-man!

EDIT:
The problem is not entierly solved:
If I try to compile as 'release' I have the same problem :(

V-man
08-14-2012, 04:01 AM
If a release version crashes and a debug version does not, then that means your code or SOIL is corrupting memory somewhere.

felixrulz
08-14-2012, 04:20 AM
If a release version crashes and a debug version does not, then that means your code or SOIL is corrupting memory somewhere.

If I just include the SOIL source files into my project both 'release' and 'debug' version work. With the self-compiled .lib file only 'debug' works. I'm not sure if this helps diagnosis? My knowledge on how linking works is not very good.