Possible memory leak

M

MSNEWS

Hi

I have a VB.NET app which reads thru a list of directories containing word
files, converts the file to text and then loads into a SQL SERVER table.

To avoid any word issues (macro virus, corrupt files, files with password)
my program spawns a new thread to perform the word conversion, and if the
thread doesn't complete within 2 seconds the thread is killed and we move
onto the next file.

Anyway the process works great for a while, but eventually I will get an
error from Word "There is not enough memory or disk space to run Word" and
my program will die. Even stranger is that when I close down all my
applications word still does not work until I reboot my PC (I use the SPY++
tool to see any strange processes but can't see anything). When my
application runs it uses around 30mb of memory and doesn't stray much from
that memory usage.

Attached is the snippet of code which does the word conversion, anybody see
anything wrong, or can anybody think of a different way to do this?

Thanks
Paul

Private Function spawnwordthread()
Dim wordthread As New System.Threading.Thread(AddressOf converttotextfile)
Dim thestart As Long = DateTime.Now.Ticks
Dim theend As Long = DateTime.Now.Ticks
Dim thefinish As Char = "N"

wordthread.Start()

While (wordthread.IsAlive And thefinish = "N")
theend = DateTime.Now.Ticks
If ((theend - thestart) / 10000000) > 2 Then
wordthread.Abort()
thefinish = "Y"
returnvalue = "THREAD ABORTED"
killwinwords()
End If
End While

Try
wordthread.Abort()
Catch ex As Exception
End Try

wordthread = Nothing

End Function

Private Sub converttotextfile()
Dim fileformat As Object
Dim theerror As String
Dim WordApp As New Word.ApplicationClass
returnvalue = "OK"

Try
WordApp.Documents.Open(filename, 0, 1)
WordApp.ActiveDocument.SaveAs(tempfile, 2)
WordApp.ActiveDocument.Close()
Catch ex As Exception
theerror = ex.Message
Try
WordApp.ActiveDocument.Close()
Catch ex2 As Exception
End Try

returnvalue = theerror
End Try

WordApp.Quit()
End Sub
 
R

Rob Windsor [MVP]

Try adding one line at the end of converttotextfile:

Private Sub converttotextfile()
...
WordApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp)
End Sub

You need to add this line to tell .NET to release the connection from your
app to Word immediately. If you don't have the call to ReleaseComObject the
COM runtime will think your app is still using Word after your done and
won't know to close it until the object reference (WordApp) gets garbage
collected. The problem is you app is active most of the time and the garbage
collector doesn't get enough cycles to clean up fast enough.

Hope this helps.
 
N

Nick Malik

Hello Paul,

The response from Rob Windsor covers your original problem.

If I can make another suggestion,
In this code, you have a while loop that simply runs, pulling the current
time, looking to see if enough time has passed.
This is very processer intensive, and may, in fact, cause you to take away
processer cycles from Word (or the GC) at a time when it needs them.

May I suggest that you add one statement to your loop:

Change
While (wordthread.IsAlive And thefinish = "N")
theend = DateTime.Now.Ticks
If ((theend - thestart) / 10000000) > 2 Then

to
While (wordthread.IsAlive And thefinish = "N")
Thread.Sleep(500) ' wait a half second
theend = DateTime.Now.Ticks
If ((theend - thestart) / 10000000) > 2 Then

I hope this helps.
--- Nick
 
M

MSNEWS

Thanks Rob and Thanks Nick. I implemented the
System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp) but I'm
still getting the problem, it looks like the process is running longer (it's
hard to tell - the crash is always at a random point), but I still
eventually get the Out of Memory in Word 10.0 message.

Right now I'm creating a new thread - and thus a new word application class
for every word document, would there be a way to open the thread, load word
and keep this thread and word open and somehow pass the filename to process
to the thread? I would still have to kill the thread in the instance where
it freezes, but that means I'm only creating new thread and application
classes in the event of word freezing, instead of every document (I have 8
million to process!)

I'm open to suggestions
Thanks
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top