tags:

views:

181

answers:

2

Alright. So I wanted to use a file written in c in c++. I ran the code in c and had absolutely no problems.

Since I don't know c, I worked with some conversion software for a while, but it wasn't effective (guessing the coding format wasn't in the style it needed). I decided to try it out myself and it looked like all I had to do was change a few malloc statements to new statments and replace free with delete. So I ran the program and it appears most of the functions work fine, but this one gives me trouble.

When I'm trying to update the display of a new image (I'm reading tiff image data and displaying it on the screen) I run into problems in these particular statements:

glutReshapeWindow(ImageWidth,ImageLength);
glClear(GL_COLOR_BUFFER_BIT);//these
glRasterPos2i(0, 0);//three
glDrawPixels(ImageWidth, ImageLength, GL_RGB,
  GL_UNSIGNED_BYTE, ImageData);//statements are the ones giving me the error

I included the first statement in the codeblock to show that it doesn't immediately blow up any any sort of OpenGl call.

I keep getting errors along the lines of

Cannot access memory at address 0xfbad248c
Cannot access memory at address 0xfbad248c
Cannot access memory at address 0xaabbeeaa

I know this might be sort of vague, but what could be some common sources of this error? It worked in c, what may have caused this error in the quick switch to c++?

Thanks and let me know if you need any more information.


Alright, so I'm not really using the standard OpenGL library I believe (this is for a class and I was given the library files by an instructor). I have the the code

#include <GL/glut.h>

as my #include. These libraries are exactly the same as the one found in the c file as I merely copied and pasted them into my c++ project. (I've also worked with them in a limited manner in another c++ project). Just in case, I checked my file system for the files "OPENGL32.DLL" and "GLU32.DLL" and did not find them.

I should probably include these additional details:

A init() method is called at the beginning of the program before the actual input loop is called:

void
init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel(GL_FLAT);
   makeCheckImage();
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}

This method seems to work fine (a simple checkerboard pattern is created shortly after this function call). I'm not sure which sort of OpenGL files these pertain to ("OPENGL32.DLL" or "GLU32.DLL") but hopefully this can narrow the problem down a little bit.

As for the ImageData line, I don't believe the ImageData object is corrupted or something of the sort, because of some of reasons you were talking about and the fact that the code in the very first code block I posted (the source of errors) is within an else block as such:

if (ImageData == NULL) {/*code*/}
else{/*this is where the error code is*/}

I can post info on the makefiles if you think it would be helpful (just don't want to clutter up this thread with the stuff if it's not).

+1  A: 

The first statement that doesn't give an error is an command from GLU32.DLL, the others should be OPENGL32.DLL commands. Check if you linked the libraries correctly and try to copy the DLLs in the program directory.

The last line could also be some error with your ImageData, but since you said it worked in C and you only replaced malloc/free with new/delete, it's unlikely that something is wrong there, anyway you should check this, too. A possibility would be to read the whole ImageData array without an OpenGL call involved. By the way, I hope that ImageLength isn't the length of the complete image file, but the height of the image.

EDIT: OK, so it seems to be a bit different. The init function you posted seems to access other gl... functions, so your libraries somehow work. By the way, it's interesting that you couldn't find the DLLs. They are usually stored in the Windows\System32 path.

"if (ImageData == NULL)" is a first step, but you should also try to read all ImageWidth*ImageLength entries for debug purposes, perhaps you allocated the wrong number of bytes.

Another reason for the crash could be that the header files you use aren't up-to-date and not compatible with your calls or the DLLs.

If all this doesn't solve your problem, posting the makefile would indeed be useful.

schnaader
I responded with an edit.
Chad
"wrong number of bytes"That's exactly right. Quite embarassing. I appreciate the help greatly though; I would have been running in circles for god knows how long looking through archived OpenGL errors.
Chad
+2  A: 

I don't know how experienced you are with the differences between C and C++, so I don't want to patronize you, but its worth a shot:

Have you used

unsigned char* data = new unsigned char[size]; //correct
//....
delete[] data;

(array allocation) or

unsigned char* data new unsigned char(size); //wrong
//....
delete data;

(allocate a single char with value size)

for your image data?

heeen