dll crash.. need ideas

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am doing some image processing work..
I have an image processing engine (written inC), which I have wrapped inside
a dll. I am calling this dll from my .net app. I am passing an image to this
dll from my app and i get the desired result. But strangely.. my .NET(C#) app
crashes after processing couple of images. But this behavior does not happen
when I am invoking the same dll from a vc++ project.
I have run may tests and determined the cause of the crash is the dll and
this strange behavior of crashing when dll is called from .net app and not
when called from vc++ app is leaving me without much clue..

can anyone throw some light on this.. ??

Regards,
 
Most likely something to do with Interop. When .NET uses native code, it
cross a boundary outside of the CLR. I am not sure whether wrapping the call
in an unsafe block and adding exception handling would cure the problem.

You might want to create a wrapper object that can Dispose() on your side,
as well, so you crash the wrapper and not the entire program.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Hi Greg,

Thanks for the reply.
I have solved the issue.. but dont seem to clearly understand the problem..
From the .NET wrapper dll, I am passing a "reference to string" to the
native dll function call. Inside the native dll call, the argument is LPTSTR,
which is a pointer to a unicode string.
Now, when the app is running, the wrapper dll (.NET)get a valid reply from
the native dll(which does the image processing), but this good reply does not
last long.. i mean.. I get good reply around 4 to 5 times and then the app
crashed..

I figured.. in the .NET code.. the string is in the function stack.. and I
dont know how .NET plays around with it.. so I put the string on the heap by
calling
"string my_string = new string(some_char_array_ref)" and then i passed
my_string's reference to the native dll call.. and boom.. my code is up and
running like a jet..!.

Can you please give me some pointers or throw some light on how the memory..
call stack and memory deallocation is handled in .NET.. it will be of much
help..

Thanks
 
Vishuonline said:
I have solved the issue.. but dont seem to clearly understand the
problem.. From the .NET wrapper dll, I am passing a "reference to
string" to the native dll function call. Inside the native dll call,

By 'reference to a string' I assume you mean that the native DLL fills a
buffer you pass to it?

Don't do it like this. System.String is immutable, you cannot change a
string once it has been created. The solution is to pass a StringBuilder
object, the interop layer will create an appropriate buffer and pass it
to the native code.

Richard
 
Back
Top