Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

Fixing managed/unmanaged problem

$
0
0

Hello,
I don't know where to post the topic, because it relates mixed applications using unamanged and managed dlls.

A lot of things have been written about loading phase. I cannot find anything about unloading phase.

We had some massive problems during termination phase of our executables.

The problem was that an unmanaged dll (3rd. party!) is using pointers to managed objects

This leads to a crash of the executable, because the unmanaged dll want's to access managed objects during unload of dll.

This has to crash, because of that phase, the CLR has already done its house keeping.

That means the the DLLDetach Code form the 3rd party library is accessing code which can not run anymore.

My understandings is the following for the initialization/deinitilization phase of mixed applications

1. crt is initiliazed

2. crt based variables are initialized (e.g. statics)

3. CLR is intialized

4. CLR base variables are initialized.

For unload it seems to be vice versa

1. CLR is doing house keeping for memory and code

2. CLR is deinitialized/unloaded

3. CRT is doing house keeping for memory and code

4. CRT is unloaded

For a unmanaged DLL (like our 3rd party library :( ), that means that it trys to call e.g. destructors being managed or using managed code. But this exatly happens after the CLR has already done its house keeping . So it crahes.

Workaround:

We found a way to tell that dll to release its internal uses objects, before the exe terminates.
That works, because all runtimes are stille availab.e

But why is Microsoft not fixing the issue.

Here is a proposal hot wo fix it.

Decouple memory house keeping from unlouding the runtimes.

Introduce an additional deinistilization phase which is cleaning the memory, but is not unloading the runtime.

After that phase, after all unmanaged heaps and all managed objects are released,

unload the runtimes.

This ensures that such cross dependent components are able to call there destucturs.

Same is true for initialization

first load both runtimes, than call dll attach and correspondin methods.

Regards

RB


Viewing all articles
Browse latest Browse all 1710

Trending Articles