473,402 Members | 2,046 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,402 software developers and data experts.

Possible memory leak

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
Jul 21 '05 #1
3 1493
Try adding one line at the end of converttotextfile:

Private Sub converttotextfile()
...
WordApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.
--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada


"MSNEWS" <xx@nospam.com> wrote in message
news:uV*************@TK2MSFTNGP09.phx.gbl...
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

Jul 21 '05 #2
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

"MSNEWS" <xx@nospam.com> wrote in message
news:uV*************@TK2MSFTNGP09.phx.gbl...
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

Jul 21 '05 #3
Thanks Rob and Thanks Nick. I implemented the
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:VNUHc.62479$XM6.36711@attbi_s53...
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

"MSNEWS" <xx@nospam.com> wrote in message
news:uV*************@TK2MSFTNGP09.phx.gbl...
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


Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Roman Mashak | last post by:
Hello, All! I have this small piece of code, where segmentation fault happenes only upon runnin code. No problems during debug (JFI I'm using gdb-6.3): ---- struct host_info { char *host;...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
3
by: Emmanuel Gehin | last post by:
When I use the following code in VB.NET : Public Function test() As String Try Dim da1 As OdbcDataAdapter Dim i As Int32 Dim tfem As DataTable For i = 0 To 1000 da1 = New...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.