473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Wrapping Word.Application object with an IDisposable class?

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
1 2884
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: jsroberts | last post by:
I have been desperately trying to solve a problem where I am wrapping an unmanaged dll with managed extensions for c++ and then calling the managed dll functions from C#. However, whenever I call...
7
by: Bonj | last post by:
Hi I made a naff web application which uses the impersonation method in MSDN (can't find it now, but it basically revolves around creating a token by calling the LogonUser API, calling...
8
by: scorpion53061 | last post by:
My boss indicated he was pleased with the reports that were being generated in MS Word by me and that made me happy. However he also told me he wants the wait eliminated for Word to start up. I...
2
by: Tim | last post by:
I have a class called Manager. In the 'Initialize' method of the Manager class, I create an instance of a COM class (say oComObject). What is the lifetime of oComObject. Will it be available as...
6
by: Gerhard Prilmeier | last post by:
Hello, I have an unmanaged C++ API that uses virtual functions, like this: class A { public: virtual void handleMe(){} };
0
by: Paul Anton Letnes | last post by:
>
0
by: Armen Kirakosyan | last post by:
HI all !! have an appication which should spell check BUT ! I cannot dynamicaly change language, or set to autodetect the server in English, but I want it spell check in German or Spanish here...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.