0

I'm trying to compile the following DLL code:

int* ptr;

DLLExport int add(int a, int b)
{
    ptr = (int*)malloc(10 * sizeof(int));
    free(ptr);

    return (a + b + 6);
}

This DLL works fine when compiled with Visual Studio, but crashes on the malloc() call when compiled with mingw/gcc. However the following code works with both compilers:

DLLExport int add(int a, int b)
{
    int* ptr;
    ptr = (int*)malloc(10 * sizeof(int));
    free(ptr);

    return (a + b + 6);
}

It also works with both compilers when I define the var as static, however that var needs to be extern and used in multiple files, so it can't be static. I tried setting it to volatile, but it didn't help.

I'm loading the DLL with Unity and it crashes with the following error:

Received signal SIGSEGV
Stack trace:
RtlLookupFunctionEntry returned NULL function. Aborting stack walk.
0x0000020f7d1669b0 (cygwin1) lfind
0x0000020f7d1c5c6b (cygwin1) acl_get_perm
<Missing stacktrace information>

cygwin1.dll is a dependency that I need to also add for the mingw DLL to be loaded (but I don't need to add any other DLLs when compiling with VC).

This is the Makefile I use for mingw:

all:
    g++ -c -o dll.o dll.cpp -O0
    g++ -c -o pch.o pch.cpp
    g++ -shared -o dll.dll ./dll3.o ./pch.o

My guess is that I'm missing some g++ flags, but I can't figure out which ones (I tried many). And these are some of the command lines that the VC compiler uses:

1>    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\CL.exe /c /ZI /JMC /nologo /W3 /WX- /diagnostics:column /sdl /Od /D _DEBUG /D DLL2_EXPORTS /D _WINDOWS /D _USRDLL /D _WINDLL /D _UNICODE /D UNICODE /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /permissive- /Zc:wchar_t /Zc:forScope /Zc:inline /Yc"pch.h" /Fp"x64\Debug\Dll2.pch" /Fo"x64\Debug\\" /Fd"x64\Debug\vc142.pdb" /external:W3 /Gd /TP /FC /errorReport:prompt pch.cpp
 
...
 
1>    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Tracker.exe /d "C:\Program Files (x86)\MSBuild\15.0\FileTracker\FileTracker32.dll" /i ...\Dll2\x64\Debug\Dll2.tlog /r ...\DLL2\PCH.CPP /b MSBuildConsole_CancelEvent806a58ad24214fb9a0f508edf8dd6bea  /c "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\CL.exe"  /c /ZI /JMC /nologo /W3 /WX- /diagnostics:column /sdl /Od /D _DEBUG /D DLL2_EXPORTS /D _WINDOWS /D _USRDLL /D _WINDLL /D _UNICODE /D UNICODE /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /permissive- /Zc:wchar_t /Zc:forScope /Zc:inline /Yc"pch.h" /Fp"x64\Debug\Dll2.pch" /Fo"x64\Debug\\" /Fd"x64\Debug\vc142.pdb" /external:W3 /Gd /TP /FC /errorReport:prompt pch.cpp
...
1>    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\link.exe /ERRORREPORT:PROMPT /OUT:"...\Dll2\x64\Debug\Dll2.dll" /INCREMENTAL /ILK:"x64\Debug\Dll2.ilk" /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:NO /manifest:embed /DEBUG /PDB:"...\Dll2\x64\Debug\Dll2.pdb" /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"...\Dll2\x64\Debug\Dll2.lib" /MACHINE:X64 /DLL x64\Debug\dllmain.obj
 
 

Many thanks in advance for any tips!

LachoTomov
  • 3,312
  • 30
  • 42
  • ¿Is `ptr` a real name of the variable or is it simplified for the sake of mcve? *"var needs to be extern and used in multiple files"* - it definitely shouldn't be. Also you write "*crashes on the malloc*", but then you write *"it crashes with the following error ..."* which has nothing to do with malloc. ¿So which one is it? – user7860670 Dec 09 '21 at 18:03
  • @user7860670 yes - ptr is the actual name. extern - I'm trying to port a large code that successfully compiles into .exe, into a DLL. In the original code that var is extern. I was hoping to be able to keep the code with minimal changes, but I don't know if it's possible to use extern inside a DLL... malloc - if I remove the malloc() and free() calls - the DLL works and successfully returns the result - that's what I mean. – LachoTomov Dec 09 '21 at 18:10
  • Maybe you forgot to copy mingw runtime dlls to the folder of your executable? – user7860670 Dec 09 '21 at 18:15
  • A note on Cygwin: It's a POSIX portability layer and you need the DLL to provide the support functions. If you don't need POSIX, and if you're building the same code in Visual Studio you almost certainly don't, you can use a MinGW compiler that doesn't need the portability layer or the DLL. [Here are instructions to install MSYS and MinGW](https://stackoverflow.com/a/30071634/4581301). MSYS includes a package manager that can provide a large ecosystem of useful tools very similar to what you can get with Cygwin without the Cygwin dependency. – user4581301 Dec 09 '21 at 18:15
  • Don't forget that you could have a bug elsewhere in the code that allows it to stagger along mortally wounded until it finally falls over somewhere completely innocent. – user4581301 Dec 09 '21 at 18:17
  • Actually I'm trying to port a POSIX code to Windows which also has some GCC intrinsics - and for that reason it doesn't compile in Visual Studio, only in mingw. Initially I hadn't copied the DLL dependencies, but then I found them - it's just cygwin1.dll. And I stripped all DLL code to just these 7 lines to make sure it's not something else. – LachoTomov Dec 09 '21 at 18:20

0 Answers0