G
Guest
Hi,
I'm using VB2005 + Office XP Enterprise.
If you create a Word.Application object, you risk a memory leak if your
application crashes because Word.Application is an unmanaged COM Interop
object.
I want to encapsulate the Word.Application in a Class that implements
IDisposable to avoid this potential memory leak. Here is the code I have:
Imports Microsoft.Office.Interop
''' <summary>
''' Encapsulates MS Word using IDisposable to prevent memory leaks. Use
WordFactory.GetWordApp to obtain a memory-safe Word.Application object
''' </summary>
''' <remarks></remarks>
Public Class WordFactory
Implements IDisposable
Public Sub New()
mobjWordApp = New Word.Application
End Sub
Private mobjWordApp As Word.Application
Public ReadOnly Property WordApp() As Word.Application
Get
Return mobjWordApp
End Get
End Property
Public Shared Function GetWordApp() As Word.Application
Dim wf As New WordFactory
Return wf.WordApp
End Function
#Region " IDisposable Support "
Private disposedValue As Boolean = False ' To detect
redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
'free managed resources when explicitly called
End If
'free unmanaged resources
On Error Resume Next
mobjWordApp.Quit()
On Error GoTo 0
End If
Me.disposedValue = True
End Sub
' This code added by Visual Basic to correctly implement the
disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Call Dispose(False)
MyBase.Finalize()
End Sub
#End Region
End Class
Now the odd thing is that it seems to work fine when compiled. Code such as
Dim w As Word.Application = WordFactory.GetWordApp
Throw New Exception("An unhandled exception was intentionally thrown
for testing purposes")
Finalizes the Word.Application object correctly (i.e. it disappears from
Task Manager), but if the program is run from the VB IDE, WINWORD.EXE stays
in Task Manager.
Is this some artefact of the debugger? Or am I doing something wrong?
I'm using VB2005 + Office XP Enterprise.
If you create a Word.Application object, you risk a memory leak if your
application crashes because Word.Application is an unmanaged COM Interop
object.
I want to encapsulate the Word.Application in a Class that implements
IDisposable to avoid this potential memory leak. Here is the code I have:
Imports Microsoft.Office.Interop
''' <summary>
''' Encapsulates MS Word using IDisposable to prevent memory leaks. Use
WordFactory.GetWordApp to obtain a memory-safe Word.Application object
''' </summary>
''' <remarks></remarks>
Public Class WordFactory
Implements IDisposable
Public Sub New()
mobjWordApp = New Word.Application
End Sub
Private mobjWordApp As Word.Application
Public ReadOnly Property WordApp() As Word.Application
Get
Return mobjWordApp
End Get
End Property
Public Shared Function GetWordApp() As Word.Application
Dim wf As New WordFactory
Return wf.WordApp
End Function
#Region " IDisposable Support "
Private disposedValue As Boolean = False ' To detect
redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
'free managed resources when explicitly called
End If
'free unmanaged resources
On Error Resume Next
mobjWordApp.Quit()
On Error GoTo 0
End If
Me.disposedValue = True
End Sub
' This code added by Visual Basic to correctly implement the
disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Call Dispose(False)
MyBase.Finalize()
End Sub
#End Region
End Class
Now the odd thing is that it seems to work fine when compiled. Code such as
Dim w As Word.Application = WordFactory.GetWordApp
Throw New Exception("An unhandled exception was intentionally thrown
for testing purposes")
Finalizes the Word.Application object correctly (i.e. it disappears from
Task Manager), but if the program is run from the VB IDE, WINWORD.EXE stays
in Task Manager.
Is this some artefact of the debugger? Or am I doing something wrong?