I created a small solution in Visual Studio 2017 to illustrate a problem I encountered. The solution contains two projects: mainapp.vcxproj
and sideapp.vcxproj
. Each project contains only a main.cpp
file.sideapp
is set as startup project.
mainapp/main.cpp
__declspec(dllexport) void testFunc()
{
char* test = new char;
delete test;
}
int main()
{
testFunc();
}
sideapp/main.cpp
__declspec(dllimport) void testFunc();
int main()
{
testFunc();
}
sideapp
is linked against mainapp.lib
and everything builds without warnings or errors. This works if mainapp
is built as a .exe
or a .dll
.
If mainapp
is built as a .dll
, sideapp
runs correctly. If it is built as an .exe
, the line
char* test = new char
triggers an exception: Exception thrown at 0x0001B4E0 in sideapp.exe: 0xC0000005: Access violation executing location 0x0001B4E0. The error persists if both projects are compiled in Release or in Debug.
The questions: Why is the behavior different between these two compilation parameters? And is there a way to avoid these exceptions without compiling mainapp
as a dll?
User contributions licensed under CC BY-SA 3.0