Calling unmanaged dll from .NET C# service causes pointer corruption eve if used InitializeLifetimeS

  • Thread starter Thread starter jarmopy
  • Start date Start date
J

jarmopy

Hi,

I have made a service with C# and calling that service class from another C#
program with remoting.
(Referendes from the calling program)
That service class is configured so that garpage collection is not used in
this class.
(singleton class + override InitializeLifetimeService )

The service class uses C++ unmanaged function from dll (using DLLimport).
The C++ function uses void pointer which is corrupted after the solution is
called some times.
The void pointer is allocated in the C++ code (dll) when one class is
allocated dynamically (with new).
The pointer is not marshaled to the service class.

I think that the problem is maybe .NET garpage collection in this case,
because same solution is working when calling from an ordinary C++ code
(unmanaged).

How is the memory handling going in this case ?
Is it possible that the .NET service code still frees the memory even if it
is in unmanaged dll ?
Should it help if the pointer is marshaled to the service singleton class or
would the situation be the same ?
(This is the next Iam going to try)


Thanks !
 
Hi,
yes it seems that when this is not called say about 2 minutes, it is not
working any more.

It gives error message:
System.NullReferenceException : Object reference not set to an instance of
an object.



Has anybody idea why this happens ?
 
Hi again,

found the reason.

When the C++ unmanaged function from dll is called, the caller is from
another thread that is whu the original void pointer is not valid any more.
I do not know why the new thread is born, but that was the original reason
for this behaviour.
 
jarmopy said:
Hi again,

found the reason.

When the C++ unmanaged function from dll is called, the caller is from
another thread that is whu the original void pointer is not valid any
more.
I do not know why the new thread is born, but that was the original reason
for this behaviour.
I don't believe this is the reason, the function you called returns a
(valid?) pointer to unmanaged memory to the caller, the fact that you use
this same pointer on another thread (which you don't know where he comes
from!) doesn't make it an invalid pointer. Supposed this was the reason,
how did you solve the issue?

Willy.
 
Hi Willy,

the correction was done in the C++ code (that what I was calling).
The guy who corrected it said that this happens if the function is called
from an another thread than the previous call. I do not know the exact
correction method but now is is working OK.
 
Back
Top