Hi guys,
There is a C++ DLL that does image processing.
It allocates 30MB in runtime and when it returns from
an __stdcall it returns a 30-50K array of integers
as C# Int32. The allocation is done with CoTaskMemAlloc.
After compilation the DLL entry name is prefixed by _
and the number of bytes of the arguments is appended
after a @ sign e.g. int __stdcall my_function(int mf_input)
is exported as the entry _my_function@4.
The DLL was checked with Bounds Checker and was compiled
with MSVC 6 and then was compiled also with MSVC 8.
It was also tested with Memory Validator.
Heap bait test was also done. 10MB allocated with the new
opeartor in an exe prior to the DLL function call. All bytes were
intiated with numbers, then a call to the DLL function was
performed and then after the return the values of the bait array
were checked. The same was done with CoTaskMemAlloc.
No change was detected. Then a stack bait test was done very
similar to the previous bait test but this time the bait array was
defined on the stack. No problems were detected !!!
The DLL works fine with C++ compiled EXE but when called from a
Dot Net web service the IIS crashes after the return.
The C# programmer knows the length of the array which is returned
by CoTaskMemAlloc after the returned pointer is cast to an int **
pointer,
int *my_array;
int my_length;
// Here code intializes my_array with 'new' and also my_length.
// The first integer in my_array is a CRC number of the rest.
// ...
e.g.
extern "C" __declspec(dllexport)
int __stdcall my_function(int **mf_output_array)
{
if (NULL==mf_output_array) return MY_ARGS_ERROR;
*mf_output_array = (int *)CoTaskMemAlloc(my_length*sizeof(int));
if (!mf_output_array)
{
delete[] my_array;
my_array = NULL;
my_length = 0;
return MY_INSUFFICIENT_MEMORY_ERROR;
}
memcpy(*mf_output_array,mf_array,my_length*sizeof( int));
delete[] my_array;
my_array = NULL;
my_length = 0;
// The calling process is responsible of calling
// CoTaskMemFree(*mf_output_array).
}
Is it possible that CoTaskMemAlloc is not Int32 aligned ?
Other allocations are usually page aligned.
How does the marshaller know the length of the returned array ?