I am working on a port of a Borland C++Builder 6 (BCB6) application to C++Builder 11 (CB11). Library code that compiles, links, and runs under BCB6 also compiles and links, but causes an access violation, under CB11. The AV doesn't occur in the library code, rather it happens when an fstream
type is instantiated somewhere else in the application - say, on the MainForm.
The AV looks like this:
Project Executable.exe raised exception class $C0000005 with message 'access violation at 0x00406af9: write of address 0x00409240'.
It pops up here in xlocale
at the specified line:
operator size_t()
{ // get stamp, with lazy allocation
if (_Id == 0)
{ // still zero, allocate stamp
_Lockit _Lock(_LOCK_LOCALE);
if (_Id == 0)
_Id = ++_Id_cnt; // <-- AV here
}
return (_Id);
}
My library unit implements a Singleton class with static member functions, but as it turns out any class with a static member function that instantiates an fstream
object anywhere (in any method) and is implemented within a static library seems to cause this issue. Based on my limited testing at least. Here is my trivial example:
LibraryUnit.h
class Foo
{
public:
static void Bar();
};
LibraryUnit.cpp
void Foo::Bar(){
ifstream in1;
}
fmMain.cpp (MainForm constructor only)
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ofstream out; // <= access violation here ...
}
If I exclude the library (.lib) from the executable, and include the library unit directly, there is no AV.
It would seem to me that this is an issue with the linker and/or library initialization.
This works fine in BCB6.
It turned out that the application and library setting for "Link with Dynamic RTL" were different.
It seems the default for a new lib is false while the default for a new exe is true.