COM Interop and memory leaks
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.

Post a new message to this list...

Dmitry Nalivkin
Good morning.

I have a next problem:
I wrote an asynchronous protocol handler in VB.NET. Another my program in
VB.NET installs it as temporary handler, creates forms with WebBrowser
control, and navigates to URI's handled by my handler. I encountered
continuous memory leaks while navigating to pages. After investigation with
SOS and memory profiler I discovered that instances of my protocol handler
class is creating at every request to a page or embedded element on it, but
freed only at program termination! Seems that unmanaged IE component creates
handlers on managed heap in my program, but garbage collector doesn't knows
anything about it, and never collect it. I've tried some tricks such as
explicitly calling finalizers and ReleaseComObject on my objects, but didn't
succeed.
Please help!

--
With best regards, Dmitry

Reply to this message...
 
    
Robert Jordan
Hi Dmitry,

[Original message clipped]

I assume you register the protocol handler with CoInternetSession's
RegisterNameSpace. RegisterNameSpace expects a IClassFactory, so
you're able to control the object creation. you can, for example,
use a managed object pool However, you'll have to deal with locking
in this case.

Bye
Rob
Reply to this message...
 
    
Dmitry Nalivkin
Hi

Thanks for replying. As I understand, I must use COM+ object pooling through
inheritance from ServiceComponent and enabling pooling through corresponding
attributes. Then handler instances in pool will be deactivated when IE
control releases references. But how RegisterNamespace will get to know that
handler uses pooling? And which locking issues I can encounter?

--
With best regards, Dmitry
"Robert Jordan" <Click here to reveal e-mail address> ???????/???????? ? ???????? ?????????:
news:cgugnf$ukm$04$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Robert Jordan
Hi Dmitry,

[Original message clipped]

No, with managed object pooling I mean you have to do your
own pooling of protocol handler instances. Just to avoid
the creation of "unlimited" instances.

Bye
Rob

[Original message clipped]

Reply to this message...
 
    
Dmitry Nalivkin
Hi

ok, now I understand. When I implemented custom class factory for my handler
I've encoutered next issue: Calling ReportData on IInternetProtocolSink
interface reference in Start method of my handler gives an error:
QueryInterface for interface Protocol.IInternetProtocolSink failed. Seems
that sink provided by IE control is not valid. When I use default class
factory, everything is ok. May be someone can help me? My class factory has
next text:

Public Class CHandlerClassFactory
Implements IClassFactory
Private Shared CLASS_E_NOAGGREGATION As Integer = &H80040110I
Private Shared E_NOINTERFACE As Integer = &H80004002I
Private ReadOnly IID_IInternetProtocol As Guid = New _
Guid("{79eac9e4-baf9-11ce-8c82-00aa004ba90b}")

Public Sub New()

End Sub

Public Sub CreateInstance(ByVal pUnkOuter As Object, ByRef riid As
System.Guid, ByRef ppvObject As System.IntPtr) Implements
ComSupport.IClassFactory.CreateInstance
ppvObject = IntPtr.Zero
If Not pUnkOuter Is Nothing Then
Marshal.ThrowExceptionForHR(CLASS_E_NOAGGREGATION)
End If

If riid.Equals(IID_IInternetProtocol) Then
Dim ctrl As ProtocolHandler = New ProtocolHandler
ppvObject = Marshal.GetComInterfaceForObject(ctrl,
GetType(IInternetProtocol))

Else
Marshal.ThrowExceptionForHR(E_NOINTERFACE)
End If
End Sub

Public Sub LockServer(ByVal fLock As Boolean) Implements
ComSupport.IClassFactory.LockServer

End Sub
End Class

--
With best regards, Dmitry
"Robert Jordan" <Click here to reveal e-mail address> ???????/???????? ? ???????? ?????????:
news:cgulpa$rtf$06$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Robert Jordan
Dmitry Nalivkin wrote:

[Original message clipped]

you have to handle aggregation. here my C#-code:

public void CreateInstance(object pUnkOuter, ref Guid riid,
out object ppvObject)
{
if (pUnkOuter != null && riid.Equals(IID_IInternetProtocol))
{
return a (maybe cached) instance of your handler
}
else
{
pUnkOuter = null;
}
}

[Original message clipped]

Reply to this message...
 
 
System.Guid
System.IntPtr
System.Runtime.InteropServices.Marshal




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com