By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 !
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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 ?

Jul 21 '05 #2

P: n/a
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.
Jul 21 '05 #3

P: n/a

"jarmopy" <ja*****@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
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.

Jul 21 '05 #4

P: n/a
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.

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.