MT4 close (crash?) on deinit() + dll threading on WinXP Mode Virtual PC

 

I have a native library built with Visual Studio 2012. Required project configuration properties:

  • General / Platform Toolset  =  Visual Studio 2012 - Windows XP (v110_xp)
  • C/C++ / Code Generation / Runtime Library  =  Multi-threaded (/MT)

ExpertSample.cpp:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mutex>

BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved) {
    return(TRUE);
}

#define MT4_EXPFUNC __declspec(dllexport)
static std::mutex mutex;

MT4_EXPFUNC void __stdcall libInit() {
    std::lock_guard<std::mutex> guard(mutex);
    OutputDebugStringA("libInit");
}
MT4_EXPFUNC void __stdcall libDeInit() {
    std::lock_guard<std::mutex> guard(mutex);
    OutputDebugStringA("libDeInit");
}

test.mq4:

#import "ExpertSample.dll"
void libInit();
void libDeInit();
#import

int init() {
    libInit();
    return(0);
}
int deinit() {
    libDeInit();
    return(0);
}
int start() {
//    libDeInit();
    return(0);
}

Testing it on Windows 7 (x64) with MetaTrader build 451 (also for build 438) it works normally.

Testing it in the WinXP Mode Virtual PC (running inside the same Windows 7), when the EA is removed from the chart, the whole terminal is closed. There is no crash report dialog, or anything in the log files.

  • If I call libDeInit() from start() it works fine.
  • If I remove the lock guard from libDeInit(), it works fine.

The above code is just a subset of a larger project. In that project, when the library was built with all kind of debug information and a lot of outputs, the problem seemed to happen less often.

 Does anybody know how to fix it, or whether it's a MetaTrader 4 or WinXP Mode Virtual PC issue?

Files:
samplelib.zip  119 kb
 

Don't know if this is relevant, but when you run it in WinXP are the libraries/dlls re-compiled to target 32-bit?

Testing it on Windows 7 (x64) with MetaTrader build 451 (also for build 438) it works normally.
 
HarriMQL4:

Don't know if this is relevant, but when you run it in WinXP are the libraries/dlls re-compiled to target 32-bit?

MetaTrader 4 is 32 bit only as far as I know (see also: https://www.mql5.com/en/forum/138294). Also how would it work in certain situations if it's not a win32 dll?
 
Metatrader is 32 bit only, but you are calling/trying to call dlls/libraries from Metatrader. You said you built the libraries using Visual Studio 2012. In the project configuration for this did you select a Win32 build configuration? If you build the dll for x64, Metatrader will not be happy.
 
HarriMQL4:
Metatrader is 32 bit only, but you are calling/trying to call dlls/libraries from Metatrader. You said you built the libraries using Visual Studio 2012. In the project configuration for this did you select a Win32 build configuration? If you build the dll for x64, Metatrader will not be happy.

Again: how would it work with the lock_guard line commented out?... But for easier deduction: yes, it is win32...
 

Hi lesliel,

Did you find a solution to the problem? Am having similar issues.

 

Regards

 

Vivek 

Reason: