Reputation: 31723
I have a managed class that uses a COM that looks like this.
Public Class MyClass
Private myobj as SomeComObject
Public Sub New()
myobj = CreateObject("SomeComObject.Class")
End Sub
Public Sub DoSomething()
dim innerobj as SomComObject.InnerClass
innerobj = myobj.CreateInnerClass("arg1", true, 5)
innerobj.DoSomething()
End Sub
End Class
Since I am using an unmanaged dll through a COM-Interob dll I am wondering if I need free the innerobj manually or if the gc is smart enough to do it automagically if I call ReleaseObject()
My class implements IDisposable and I do the following atm:
Runtime.InteropServices.Marshal.ReleaseComObject(myobj)
Do I need to take care of releasing all inner objects that are created by the COM Object or not? And If I do have to do it, does the order matter (first inner then parent vs parent then inner)?
Upvotes: 3
Views: 353
Reputation: 2155
COM will not Release the inner object. It is an instantiation of an object whose class happens to belong to another class. Unless there is explicit code in the outer object (myObj
) to keep a handle to the created inner object (created in the call CreateInnerClass
), COM doesn't know to free it. There could be code in the myObj
type to do this, but that's not typical.
Upvotes: 1
Reputation: 55009
I'm fairly certain that you should release them all separately. At least when using Office Automation it's a common reason for things to get stuck in memory when you don't release all the inner objects which is very easy to do in Office where there are so many collections.
I'd assume that it would be the same for other COM objects.
Upvotes: 1