Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

COM Object DLL never unloads

$
0
0

Hello,

I have created a -very- basic ATL COM DLL with one ATL simple object in it.

It was generated using the default settings for a Microsoft Visual Studio 2008 ATL (DLL) project

Here is the IDL file for it:

[

      object,

      uuid(8C25269F-2F39-433B-85B3-DE91E3A9BD32),

      dual,

      nonextensible,

      helpstring("I_AA_SimpleObject Interface"),

      pointer_default(unique)

]

interface I_AA_SimpleObject : IDispatch{

      [id(1), helpstring("method Method")] HRESULT Method(void);

};

[

      uuid(FD10B1A1-3CE4-4E19-A7C6-6CD96ECB7421),

      version(1.0),

      helpstring("ATL_Main 1.0 Type Library")

]

library ATL_MainLib

{

      importlib("stdole2.tlb");

      [

             uuid(B245EB27-C7B9-4663-AA02-E2647132B97B),

             helpstring("_AA_SimpleObject Class")

      ]

      coclass _AA_SimpleObject

      {

             [default] interface I_AA_SimpleObject;

      };

};

Here is the .h file for the simple object

// _AA_SimpleObject.h : Declaration of the C_AA_SimpleObject

#pragma once

#include "resource.h"      // main symbols

#include "ATL_Main_i.h"

// C_AA_SimpleObject

class ATL_NO_VTABLE C_AA_SimpleObject :

      public CComObjectRootEx<CComSingleThreadModel>,

      public CComCoClass<C_AA_SimpleObject, &CLSID__AA_SimpleObject>,

      public IDispatchImpl<I_AA_SimpleObject, &IID_I_AA_SimpleObject, &LIBID_ATL_MainLib, /*wMajor =*/ 1, /*wMinor =*/ 0>

{

public:

      C_AA_SimpleObject(){}

DECLARE_REGISTRY_RESOURCEID(IDR__AA_SIMPLEOBJECT)

BEGIN_COM_MAP(C_AA_SimpleObject)

      COM_INTERFACE_ENTRY(I_AA_SimpleObject)

      COM_INTERFACE_ENTRY(IDispatch)

END_COM_MAP()

      DECLARE_PROTECT_FINAL_CONSTRUCT()

      HRESULT FinalConstruct(){ return S_OK; }

      void FinalRelease(){}

public:

      STDMETHOD(Method)(void);

};

OBJECT_ENTRY_AUTO(__uuidof(_AA_SimpleObject), C_AA_SimpleObject)

Here is the .cpp file for the simple object

// _AA_SimpleObject.cpp : Implementation of C_AA_SimpleObject

#include "stdafx.h"

#include "_AA_SimpleObject.h"

// C_AA_SimpleObject

STDMETHODIMP C_AA_SimpleObject::Method(void)

{

      AFX_MANAGE_STATE(AfxGetStaticModuleState());

      return S_OK;

}

I have also created a very simple winform C# application (Visual Studio 2008), using all defaults for project

I have added a reference to the ATL Project (above) to this winform application, then create buttons to create, call the exposed method and then to set the object to null (hopefully to unload the DLL as well)

Here is the code for that:

       ATL_MainLib._AA_SimpleObjectClass m_aa = null;

       private void button_Create_Click(object sender, EventArgs e)

       {

           m_aa = new ATL_MainLib._AA_SimpleObjectClass();  

       }

       private void button_Init_Click(object sender, EventArgs e)

       {

           m_aa.Method();

       }

       private void button_TearDown_Click(object sender, EventArgs e)

       {

           m_aa = null;

       }

When I start debugging the C# application, I look at the modules window and see the "Atl_Main.dll" is NOT loaded.

When I click the create button, the DLL is loaded.

I click the Init button and I can hit the break point I set in the "Method" function of the ATL object.

When I click the TearDown button, the object is set to null and I expect the "Atl_Main.dll" to unload and all associated resources to be freed as well... but it is not.

I have even tried using Marshal.ReleaseComObject, but the DLL still remains in memory.

       private void button_TearDown_Click(object sender, EventArgs e)

       {

           int nCount = Marshal.ReleaseComObject(m_aa);

           m_aa = null;

       }

Can anybody unload my DLL?

Thanks


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>