c# - COM RCW reference count in an AppDomain -
i have outlook addin i'm loading separate appdomains into. i'm passing outlook (com rcw) objects these appdomains. basics of work fine, reference counts seem come unstuck when fetch object reference within appdomain.
for straight-forward example, i've got button on mail.compose ribbon, click handler. code executing within primary outlook add-in appdomain.
var execasm = system.reflection.assembly.getexecutingassembly(); var loaduri = new uri(execasm.codebase); string loaddir = system.io.path.getdirectoryname(loaduri.localpath); // create app-domain appdomainsetup appsetup = new appdomainsetup(); appsetup.applicationbase = loaddir; system.security.permissionset permset = new system.security.permissionset( system.security.permissions.permissionstate.unrestricted); var mydomain = appdomain.createdomain("mydomain", null, appsetup, permset, null); // instance app-domain dynamic addinpoint = mydomain.createinstanceandunwrap( assemblyname: "classlibrary1", typename: "classlibrary1.class1"); // work on mail item outlook.mailitem currentmail = outlook.mailitem)globals.thisaddin.application.activeinspector() .currentitem; addinpoint.dowork(currentmail); // clean-up marshal.releasecomobject(currentmail); currentmail = null;
and class
implementation below. clear, ever executed within separate appdomain.
public void dowork(outlook.mailitem mail) { mail.save(); mail.to = "foobar@test.com"; var propaccessor = mail.propertyaccessor; marshal.releasecomobject(propaccessor); propaccessor = null; }
the usage scenario straight forward:
- click "new email".
- click add-in button in ribbon.
- close email - choose "no" on unsaved changes prompt.
- close outlook - receive 2 more "unsaved changes" prompts same item, indicative of un-released reference mail item.
the issue appears lie in call mail.propertyaccessor
, , subsequent releasecomobject
. should releasing reference, , refcount returned releasecomobject
is zero, there's still reference, removing lines dowork
method results in no "unsaved changes" prompt @ step 4. it's worth, moving more brute-force marshal.finalreleasecomobject
method makes no difference.
there's nothing intrinsically wrong mail.propertyaccessor
code. move out of dowork
method , ribbon button click event handler, , 1 expect, there no unsaved changes prompts @ step 4.
what's going on here?
edit 1
it seems make difference what property accessed within separate appdomain. propertyaccessor
causes problems, recipients
not. session
, application
, attachments
ok. in fact far, mailitem property i've found trouble propertyaccessor
.
Comments
Post a Comment