Discussion:
Calling DLL in multiple threads
(too old to reply)
Sarah
2011-03-28 01:47:10 UTC
Permalink
First, I'm quite new to multithread programming.

I have an old DLL written in Delphi that works great on a single
thread, but when called in VB.NET from a Parallel.For loop I get an
error "Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."

There are 4 parameters. 2 pChar strings passed in, and 2 pChar
strings returned with the results. stdCall is used. There are no
global variables, and 1 Threadvar.

I compiled the same code in Free Pascal. It behaves the same way.
Works fine with 1 thread, same error when called in the Parallel.For.

Any guesses?
tia
Maarten Wiltink
2011-03-28 07:31:17 UTC
Permalink
Post by Sarah
First, I'm quite new to multithread programming.
I have an old DLL written in Delphi that works great on a single
thread, but when called in VB.NET from a Parallel.For loop I get an
error "Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."
There are 4 parameters. 2 pChar strings passed in, and 2 pChar
strings returned with the results. stdCall is used. There are no
global variables, and 1 Threadvar.
I compiled the same code in Free Pascal. It behaves the same way.
Works fine with 1 thread, same error when called in the Parallel.For.
Any guesses?
Does the DLL allocate or deallocate storage for the values in the PChars?

Groetjes,
Maarten Wiltink
Sarah
2011-03-28 23:31:03 UTC
Permalink
Post by Maarten Wiltink
Post by Sarah
First, I'm quite new to multithread programming.
I have an old DLL written in Delphi that works great on a single
thread, but when called in VB.NET from a Parallel.For loop I get an
error "Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."
There are 4 parameters.  2 pChar strings passed in, and 2 pChar
strings returned with the results.  stdCall is used.  There are no
global variables, and 1 Threadvar.
I compiled the same code in Free Pascal.  It behaves the same way.
Works fine with 1 thread, same error when called in the Parallel.For.
Any guesses?
Does the DLL allocate or deallocate storage for the values in the PChars?
Groetjes,
Maarten Wiltink
Thanks for the reply.
No. The VB.NET allocates them on the stack (local variables), I
should have mentioned that in the original post.
In the Pascal I use strMove to copy the results either into the result
string, or the error string, depending on success.
Using the debugger in VB.NET, I can slow the code way down so there is
plenty of time in between calls so I know there is no race conflict.
It seems that calls from one thread is messing with another thread.
My understanding is, the only way that can happen is if the DLL used
global variables, or if the calling program didn't pass in unique
variables to the DLL on each thread.
Jamie
2011-03-30 01:16:45 UTC
Permalink
Post by Sarah
First, I'm quite new to multithread programming.
I have an old DLL written in Delphi that works great on a single
thread, but when called in VB.NET from a Parallel.For loop I get an
error "Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."
There are 4 parameters. 2 pChar strings passed in, and 2 pChar
strings returned with the results. stdCall is used. There are no
global variables, and 1 Threadvar.
I compiled the same code in Free Pascal. It behaves the same way.
Works fine with 1 thread, same error when called in the Parallel.For.
Any guesses?
tia
Look into using a TcriticalSection to block the entrance of the code
block..

Jamie

Loading...