Domanda

I'm currently porting a software from 32 to 64-bits using Visual Studio 2008 and I'm encountering an issue regarding fread which causes a segfault when called:

Here is a code sample reproducing this issue:

void somefunction(std::string filepath)
{
    FILE* myfile = fopen(filepath.c_str(),"rb"); // returns a valid handle
    if (myfile)
    {
        char* buffer = new char[BUFFER_SIZE+1];
        memset(buffer,0,BUFFER_SIZE+1);
        fread(buffer,1,BUFFER_SIZE,myfile); // segfault happens here
        fclose(f);
    }
}

GetError and ferror do not report any error and the file can be read when compiling for 32-bits. It always triggers a segfault when entering fread on 64-bits though.

I've tried several other file reading methods (with ifstream and Qt's QFile) and they work.

Unfortunately, fread is used in many other places in the code and I would like to know if there is something peculiar with its implementation for VS2008 64-bit before changing every bit of code that uses it.

Thanks in advance.

È stato utile?

Soluzione

Ok guys I think I figured it out.

Seems this issue was related with msvcrt linking on Debug mode.

The fread call was located inside a library linked against msvcrtd.lib while the executable from where the error was raised was linked against msvcrt.

I'm kind of surprised this was only causing issues with fread. Are the libc symbols located inside msvcrt under Windows ?

Anyway, I'm currently cleaning my link flags. Sorry not to have been able to post a more detailed code sample. We are working under very strict NDAs and I did not excpect this to be related to the link flags ^^'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top