By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Wrapping Word.Application object with an IDisposable class?

P: n/a
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?
Oct 10 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hello,

Would you mind letting me know the result of the suggestions? How is the
issue in your side? Is it a Windows form project or a console one? If you
need further assistance, feel free to let me know. I will be more than
happy to be of assistance.

Have a great day!

Sincerely,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 16 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.