0

I'm loading a texture image:

//glGenTextures... glBindTexture...
BYTE* image = (BYTE*) malloc(256 * 256 * 3);
FILE* imageFile;
errno_t err = fopen_s(&imageFile, fileName, "rb");
if (err != 0){
    perror("Error loading file");
    return false;
}
fread(image, 256 * 256 * 3, 1, imageFile);
fclose(imageFile);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, image);

I am building it in VS2013, and it always works when I execute it in VS. However when I copy out the executable and run it there, it sometimes works which is strange. I have completely no idea why it is happening.

The texture is 256x256 in .data format (raw data).

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • Your enums should start with GLU instead of GL to begin with. Not sure if that changes anything. – Andreas Mar 31 '16 at 05:26
  • @Andreas If I change them to `GLU_`, none of them are declared. – Derek 朕會功夫 Mar 31 '16 at 05:29
  • https://www.opengl.org/sdk/docs/man2/xhtml/gluBuild2DMipmaps.xml – Andreas Mar 31 '16 at 05:29
  • @Andreas I believe it is different on Windows actually: https://msdn.microsoft.com/en-us/library/windows/desktop/dd368652(v=vs.85).aspx – Derek 朕會功夫 Mar 31 '16 at 05:30
  • Ok. At least I learned something today :-) Could you give more details in your post regarding system (windows) and what you mean by "sometimes" and "works". – Andreas Mar 31 '16 at 05:37
  • @Andreas When the program crashes at `gluBuild2DMipmaps` I simply kill it and execute it again. Repeat this process and the program will eventually run with expected behaviors (no crash, texture are loaded correctly). When the program is run in VS, it never crashes. This is on Windows 8.1. – Derek 朕會功夫 Mar 31 '16 at 05:41
  • You should also check for opengl errors before and after the call you find suspicious when debugging. Maybe you get an OUT_OF_MEMORY error in which case your context is in an undeterminate state. – Andreas Mar 31 '16 at 05:43
  • @Andreas Apparently the heap is corrupted (checked with `_heapchk() != _HEAPOK`) and most probably the problem lies before `gluBuild2DMipmaps` is executed... – Derek 朕會功夫 Mar 31 '16 at 05:51
  • @Andreas Got it figured out... The reason the memory is corrupted is because of my call to `glLightfv`. – Derek 朕會功夫 Mar 31 '16 at 05:57
  • First of all, check value returned by `fread()`, it can be something wrong with file – VolAnd Mar 31 '16 at 06:02
  • While we're on the topic of heap corruption... you realize the failure case here leaks 192 KiB of memory? You should probably check to see if the file is readable and 192 KiB or larger before you allocate that buffer ;) – Andon M. Coleman Apr 04 '16 at 02:05
  • @AndonM.Coleman Ah, thanks for catching that. – Derek 朕會功夫 Apr 04 '16 at 02:07

0 Answers0