Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Any method to create a event like DLL_PROCESS_DETACH? #50

Closed
marcussacana opened this issue Sep 1, 2017 · 5 comments
Closed
Labels

Comments

@marcussacana
Copy link

I wanna stop a memory leak, no ideia to do this without a event while end the process.

@3F
Copy link
Owner

3F commented Sep 1, 2017

Therefore, the easy way to aggregate control of this manually via Observer pattern etc. (unmanaged / managed side)
Also if you have logic from unmanaged code that will be executed with managed env, try to use Conari.

@3F 3F added the question label Sep 1, 2017
@marcussacana
Copy link
Author

marcussacana commented Sep 1, 2017

Thank you, this Conari looks interesting, should I close the issue or keep it open for future reference?

--Edit, I used the AppDomain.ProcessExit event, he is called but he have a "timeout" to free the memory... if the event exceed this, he is interrupted... (3 secs in my target process, i don't know if this can change)

@3F
Copy link
Owner

3F commented Sep 4, 2017

Leave it open if your problem still is actual.

  • Opened issues may be reconsidered later again by me or anyone else.
  • After closing it still may be referenced & commented without any problems.

@3F
Copy link
Owner

3F commented Sep 25, 2017

@marcussacana just noticed your new edit (please note, github will never send notification for edit)

I do not recommend to use AppDomain.ProcessExit event because it doesn't give guarantee for this operation.

Also, the timeout settings available only for unmanaged api, e.g.:

ICLRPolicyManager* clrpm;
if(FAILED(pControl->GetCLRManager(__uuidof(ICLRPolicyManager), (void**)&clrpm))) {
    return -1;
}

// https://docs.microsoft.com/en-us/dotnet/framework/unmanaged-api/hosting/eclroperation-enumeration
clrpm->SetTimeout(OPR_ProcessExit, 30000); //30sec

Thus, try to implement any your custom manager. I don't know what you do, but for example:

  • Extract a common interface like IUnmanagedResource which will contain methods for allocation and release, like Alloc(), Free() etc.
  • Implement a common manager that will aggregate all IUnmanagedResource and provide a common methods through pointer to struct for other side.

Define a Host side that will manage resources: if clr side is unstable (domain may be destructed in any time without notice the unmanaged side) you should share it from unmanaged for managed side (but it can be implemented vice versa) for any allocation of resources.

The managed module can request allocation of resources from unmanaged side and access to this data via pointer to structures etc. Conari also may help to work with this data even without declaration of this structures at all, for example:

unmanaged C++

    struct TSpecB
    {
        bool d;
        TSpecA* s;
    };

    struct TSpecA
    {
        int a;
        int b;
    };

    A->a = 4;
    A->b = -8;
    B->d = true;
    B->s = TSpecA*;

Conari (illustration of native raw accessing without declaration):

[DllExport]
public static void mypoint(IntPtr ptr)
{
    var TSpecBPtr = NativeData
                        ._(ptr)
                        .t<bool>("d")
                        .t<IntPtr>("s")
                        .AlignSizeByMax;


    ...
    dynamic dlr = TSpecBPtr.Raw.Type;

    IntPtr addrA = dlr.s;
    Assert.AreEqual(true, dlr.d);

    // B->A

    var TSpecAPtr = NativeData
                        ._(addrA)
                        .align<Int32>(2, "a", "b");

    dynamic s = TSpecAPtr.Raw.Type;

    Assert.AreEqual(4, s.a);  // B->s->a
    Assert.AreEqual(-8, s.b); // B->s->b

...

Therefore, even if domain of some this loaded module will be destroyed (unexpectedly for unmanaged side) you can still release it via known IUnmanagedResource methods.

@3F
Copy link
Owner

3F commented Jul 16, 2018

I closed this issue. Please reopen if this still is actual.

About:

I wanna stop a memory leak,

You can also look #78

@3F 3F closed this as completed Jul 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants