cvisual-studio-2012icuvisual-c++

ICU u_fgetfile incompatible with runtime in release builds for VS2012


I'm trying to pass the handle returned from u_fgetfile into fseek/fread functions.

When linking my application with the debug runtime libraries (/MTd /MDd) there is no crash, but if I link against the static versions this simple code crashes:

#include <stdio.h>
#include "unicode\ustdio.h"

int main()
{
    UFILE* file;
    file = u_fopen("C:\\test.txt","r",NULL,"UTF-8");
    fseek(u_fgetfile(file),3,SEEK_SET);
}

Now this happens with both official builds of ICU and when I build custom builds with Visual Studio 2012 (building ICU in debug or release doesn't matter).

The only thing I have found out is that there seems to be some mismatch in the FILE structure, but I really don't know.


Solution

  • The problem is "icuio51.dll" (Release) is linked against the static CRT! Therefore it does not share the same FILE pointer with the shared CRT! And that´s the reason why it crashes in "lock"...

    On the other hand: ´icuio51d.dll´ (Debug) is linked against the same shared CRT (msvcr110d.dll) and therefor are using the same shared FILE* pointer.

    That´s the reason while it is working in "Debug" but not in "Release".

    Solution: You have to recompile the ICU with the correct settings for always using "Shared CRT" (/MD and /MDd)... To do this, you have to make the follwoing steps:

    1. Open the Solution "allinone\allinone.sln"
    2. Edit the properties of the project "io"
    3. Change "C/C++|Code Generation|Runtime Library" from "Multi-threaded (/MT)" to "Multi-threaded DLL (/MD)" for "Win32" Platform (x64 seems to be correct!)
    4. Recompile your project