开发者

Access violation in Release Mode

开发者 https://www.devze.com 2023-03-27 14:48 出处:网络
I get an access violation error in release mode, but not debug mode. The error occurs when I try to close a file 开发者_如何学编程which was opened to read data from. This is the code:

I get an access violation error in release mode, but not debug mode.

The error occurs when I try to close a file 开发者_如何学编程which was opened to read data from. This is the code:

FILE *file;
GLubyte *transferFunctionData = NULL;
transferFunctionData = new GLubyte(size);

if ( (file = fopen(fileName, "rb")) == NULL)
{
    printf("Cannot open file.\n");
    exit(1);
}

if ( fread(transferFunctionData, sizeof(GLubyte), size, file) != size)
{
    if (feof(file))
         printf("Premature end of file.");
    else
        printf("File read error.");

    exit(1);
}

fclose(file);

What is interesting is that it changes the values in a pointer to a vector of pointers. Not sure if, I'm saying that correctly, this is the data container

vector<CustomObject*> *data;

In Visual Studio, I add a watch for this container. When the program tries to close the file, in the above code, it invalidates all the values stored in the container, and crashes.

Both sets of code are not related, in are not even part of the same object, so this suggests to me that the heap is getting corrupted at some stage.

But why only in release mode, is this due to release mode optimizing the code, which debug mode does not?


you should use either

transferFunctionData = new GLubyte[size];

if you want allocate array of GLubytes or

fread(transferFunctionData, sizeof(GLubyte), 1, file)

if you want to allocate and read one. Right now you are allocating one GLubyte and reading size, overwriting unallocated memory


I believe it's enough to enable /EHa option in Release project settings (likely it's enabled in Debug). See Project Properties -> C/C++ -> Code Generation -> Modify the Enable C++ Exceptions to "Yes With SEH Exceptions". That's it!

See details here: http://msdn.microsoft.com/en-us/library/1deeycx5(v=vs.80).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消