DLL Implementation

 

I wrote a DLL that is being used by two different EAs. The DLL is in the library folder. I ran both EAs on the same MT4 instance on separate charts. MT4 experienced a crash.

I suspect that the crash was caused by MT4 loading the same EA from the different charts... in essense loading the same EA twice. Does this sound plausible?

 
why don't u run in 1 ea to check?
 
chiwing wrote >>
why don't u run in 1 ea to check?

I've experienced the same problem. Place a second copy of the dll in the library folder. Use a different name for each copy of the dll.

 
SquareRoot:

[...] Does this sound plausible?

Not in itself. There's no reason why a DLL can't work with multiple EAs calling it simultaneously. If you have a problem which is solved by using multiple copies of the same DLL, then you probably have an inherent weakness in the DLL which has the potential to recur with a single EA, particularly given that every single call to the DLL from MT4 comes from a different thread.

 
jjc:

Not in itself. There's no reason why a DLL can't work with multiple EAs calling it simultaneously. If you have a problem which is solved by using multiple copies of the same DLL, then you probably have an inherent weakness in the DLL which has the potential to recur with a single EA, particularly given that every single call to the DLL from MT4 comes from a different thread.

Thanks for the comments.


I've been experimenting. I have 2 separate broker MetaTraders running. The first broker has only one chart using the DLL. The second broker has 2 charts using same DLL. I started them running last night. This morning the first broker is still running. The second broker crashed with error message "Memory could not be read".

Does this error message point in any specific direction?


I will also be trying to rename the DLL to see if it makes a difference.

 
SquareRoot:

Does this error message point in any specific direction?

Not really. It depends on what your DLL is doing. For example, anything along the lines of the code below is going to land you in trouble.


A further test would be to put the DLL in System32 rather than in each MT4's experts\libraries directory, and run one chart on each copy of MT4. If they don't blow up, despite the fact they're using the same DLL, then your problem is related to having two clients of the DLL in the same process address space.


Assuming that your DLL code gets called on every tick - i.e. each call to start() - then it's interesting that the problem only occurs occasionally ("overnight") rather than almost immediately. That confirms that the issue isn't with having two EAs using the same DLL; it's to do with some assumption in your code which is occasionally getting broken by having two simultaneous callers.


Example very, very bad code:


char * glbStorePointerFromFunction1;


int __stdcall Function1(char * x)

{

// Put a memory reference into a global variable which is shared across functions

glbStorePointerFromFunction1 = x;

}


int __stdcall Function2()

{

// Do something with glbStorePointerFromFunction1

}

 
jjc:

Not really. It depends on what your DLL is doing. For example, anything along the lines of the code below is going to land you in trouble.


A further test would be to put the DLL in System32 rather than in each MT4's experts\libraries directory, and run one chart on each copy of MT4. If they don't blow up, despite the fact they're using the same DLL, then your problem is related to having two clients of the DLL in the same process address space.


Assuming that your DLL code gets called on every tick - i.e. each call to start() - then it's interesting that the problem only occurs occasionally ("overnight") rather than almost immediately. That confirms that the issue isn't with having two EAs using the same DLL; it's to do with some assumption in your code which is occasionally getting broken by having two simultaneous callers.


Example very, very bad code:


char * glbStorePointerFromFunction1;


int __stdcall Function1(char * x)

{

// Put a memory reference into a global variable which is shared across functions

glbStorePointerFromFunction1 = x;

}


int __stdcall Function2()

{

// Do something with glbStorePointerFromFunction1

}


 
SquareRoot:


I think I have isolated the problem. I had 2 copies of the same EA on EURUSD charts on the same MT4 instance plus a third EA on GBPJPY chart on this same instance.

The 2 copies of the EURUSD EA were running different indicator options. I think these 2 EAs called the same DLL function at the same time.

The reason I think this ... is because I removed the second EURUSD EA and MT4 hasn't crashed yet.


Question ... can a MT4 crash be caused by 2 EAs calling the same DLL function at the same time?


The DLL is not very complex. It is made up of 2 independent functions. One function is called on every tick. This function returns an integer to the EA. The EA uses stdcall. The error message was "Memory could not be read", which leads me to believe that the problem may not be the code itself.

 
SquareRoot:

I think I have isolated the problem. I had 2 copies of the same EA on EURUSD charts on the same MT4 instance plus a third EA on GBPJPY chart on this same instance.

The 2 copies of the EURUSD EA were running different indicator options. I think these 2 EAs called the same DLL function at the same time.

The reason I think this ... is because I removed the second EURUSD EA and MT4 hasn't crashed yet.


Question ... can a MT4 crash be caused by 2 EAs calling the same DLL function at the same time?


The DLL is not very complex. It is made up of 2 independent functions. One function is called on every tick. This function returns an integer to the EA. The EA uses stdcall. The error message was "Memory could not be read", which leads me to believe that the problem may not be the code itself.


I'm having the same problem..

If my EA that uses the same DLL is running in 2 charts, then the MT4 crashes...

OR

IF my EA is running in 1 chart, however, I'm doing the backtest, then the system also crashes.

Some real solution, or just "quick fix"/"workaround"?

 
bearnaked:


I'm having the same problem..

If my EA that uses the same DLL is running in 2 charts, then the MT4 crashes...

OR

IF my EA is running in 1 chart, however, I'm doing the backtest, then the system also crashes.

Some real solution, or just "quick fix"/"workaround"?


I found the solution guys for Delphi, then you must find the solution for you language.

ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters.

Bye

Reason: