473,737 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with "handles" - possible garbage collection issue?

I have a problem in my application which I believe is due to open handles.. .

The symptom that users report is that after they have been using the application for a while, it will randomly just crash with an exception report (I've not got the details of the error, but I'm working on that now!).

I'm trying to reproduce the circumstances by simulating a typical batch of user tasks on my pc and monitoring whats happening. I'm guessing it's some problem with threads/handles or something so I've opened up the task manager and added all the available columns.

What I've noticed is that whilst the application is running, the memory usage keeps increasing, but more worrying, the no of open handles just keeps going up and up (it's nearly 4000 handles after running through about 30 tasks).

What the application is doing is as follows:

I have a main form showing, which displays a list from a database.

Then what I'm doing is clicking down the list one by one..

What this does is to open a form on another thread to stop it blocking the main window. It does this as follows

=====
Private sub Click(rowno as int32)
dim vehicle as new dmsvehicle.vehi cle
vehicle.showveh icle(Data(rowno ))
end sub
=====

The Vehicle.showveh icle subroutine is as follows:

====
public sub ShowVehicle(Sto ckNo)
Dim frm As New ViewVehicleStat us(StockNo)

Dim t As New Threading.Threa d(AddressOf frm.dmsShowDial og)

'frm.StockNo = StockNo

t.Start()

end Sub

====

The "dmsShowDia log" subroutine in the form is simply doing a me.showdialog.

This all seems to work fine, and when I close the form I see a message such as "The thread '<No Name>' (0x6ec) has exited with code 0 (0x0)." appear in the output window of the development environment.

However, the no of handles just keeps increasing - I presumed that the garbage collector should tidy up every now and then, but thats not what I'm seeing.

I've not actually duplicated the users issue yet (but I have a high spec machine with masses of memory), but I suspect that this will be the cause of their problem?

Any suggestions as to what I've done wrong?

Thanks in advance

Simon


Nov 21 '05 #1
7 2630
"Simon Verona" <ne**@aphrodite uk.com> schrieb
I have a problem in my application which I believe is due to open
handles.. .

The symptom that users report is that after they have been using the
application for a while, it will randomly just crash with an
exception report (I've not got the details of the error, but I'm
working on that now!).

I'm trying to reproduce the circumstances by simulating a typical
batch of user tasks on my pc and monitoring whats happening. I'm
guessing it's some problem with threads/handles or something so I've
opened up the task manager and added all the available columns.

What I've noticed is that whilst the application is running, the
memory usage keeps increasing, but more worrying, the no of open
handles just keeps going up and up (it's nearly 4000 handles after
running through about 30 tasks).

What the application is doing is as follows:

I have a main form showing, which displays a list from a database.

Then what I'm doing is clicking down the list one by one..

What this does is to open a form on another thread to stop it
blocking the main window. It does this as follows

=====
Private sub Click(rowno as int32)
dim vehicle as new dmsvehicle.vehi cle
vehicle.showveh icle(Data(rowno ))
end sub
=====

The Vehicle.showveh icle subroutine is as follows:

====
public sub ShowVehicle(Sto ckNo)
Dim frm As New ViewVehicleStat us(StockNo)

Dim t As New Threading.Threa d(AddressOf frm.dmsShowDial og)

'frm.StockNo = StockNo

t.Start()

end Sub

====

The "dmsShowDia log" subroutine in the form is simply doing a
me.showdialog.

This all seems to work fine, and when I close the form I see a
message such as "The thread '<No Name>' (0x6ec) has exited with code
0 (0x0)." appear in the output window of the development
environment.

However, the no of handles just keeps increasing - I presumed that
the garbage collector should tidy up every now and then, but thats
not what I'm seeing.

I've not actually duplicated the users issue yet (but I have a high
spec machine with masses of memory), but I suspect that this will be
the cause of their problem?

Any suggestions as to what I've done wrong?

Thanks in advance

You are creating the Form in the wrong thread. You must create it in the
thread that you started and that calls Showdialog.

I don't know if it solve the handles problem. Maybe you don't call Dispose
on all created resources.

Armin

Nov 21 '05 #2
I'm deliberately creating the form on the new thread to make the call to
open the form non-blocking. If I didn't create a new thread, then with the
new form open, the original form would block awaiting it to close.

Am I doing this wrong? Is there an alternative way to do this?

I don't use the "dispose" method at all in my code, I presumed that when the
thread died, the garbage collector would tidy up all the resources used by
that thread.. Have I got this wrong?

Regards
Simon
"Armin Zingler" <az*******@free net.de> wrote in message
news:eN******** ******@TK2MSFTN GP09.phx.gbl...
"Simon Verona" <ne**@aphrodite uk.com> schrieb
I have a problem in my application which I believe is due to open
handles.. .

The symptom that users report is that after they have been using the
application for a while, it will randomly just crash with an
exception report (I've not got the details of the error, but I'm
working on that now!).

I'm trying to reproduce the circumstances by simulating a typical
batch of user tasks on my pc and monitoring whats happening. I'm
guessing it's some problem with threads/handles or something so I've
opened up the task manager and added all the available columns.

What I've noticed is that whilst the application is running, the
memory usage keeps increasing, but more worrying, the no of open
handles just keeps going up and up (it's nearly 4000 handles after
running through about 30 tasks).

What the application is doing is as follows:

I have a main form showing, which displays a list from a database.

Then what I'm doing is clicking down the list one by one..

What this does is to open a form on another thread to stop it
blocking the main window. It does this as follows

=====
Private sub Click(rowno as int32)
dim vehicle as new dmsvehicle.vehi cle
vehicle.showveh icle(Data(rowno ))
end sub
=====

The Vehicle.showveh icle subroutine is as follows:

====
public sub ShowVehicle(Sto ckNo)
Dim frm As New ViewVehicleStat us(StockNo)

Dim t As New Threading.Threa d(AddressOf frm.dmsShowDial og)

'frm.StockNo = StockNo

t.Start()

end Sub

====

The "dmsShowDia log" subroutine in the form is simply doing a
me.showdialog.

This all seems to work fine, and when I close the form I see a
message such as "The thread '<No Name>' (0x6ec) has exited with code
0 (0x0)." appear in the output window of the development
environment.

However, the no of handles just keeps increasing - I presumed that
the garbage collector should tidy up every now and then, but thats
not what I'm seeing.

I've not actually duplicated the users issue yet (but I have a high
spec machine with masses of memory), but I suspect that this will be
the cause of their problem?

Any suggestions as to what I've done wrong?

Thanks in advance

You are creating the Form in the wrong thread. You must create it in the
thread that you started and that calls Showdialog.

I don't know if it solve the handles problem. Maybe you don't call Dispose
on all created resources.

Armin

Nov 21 '05 #3
Why dont you try a form.dispose, when you return from the showdialog and see
if that solves the handle problem?

--
Rgds,
Anand
VB.NET MVP
http://www.dotnetindia.com
"Simon Verona" wrote:
I have a problem in my application which I believe is due to open handles.. .

The symptom that users report is that after they have been using the application for a while, it will randomly just crash with an exception report (I've not got the details of the error, but I'm working on that now!).

I'm trying to reproduce the circumstances by simulating a typical batch of user tasks on my pc and monitoring whats happening. I'm guessing it's some problem with threads/handles or something so I've opened up the task manager and added all the available columns.

What I've noticed is that whilst the application is running, the memory usage keeps increasing, but more worrying, the no of open handles just keeps going up and up (it's nearly 4000 handles after running through about 30 tasks).

What the application is doing is as follows:

I have a main form showing, which displays a list from a database.

Then what I'm doing is clicking down the list one by one..

What this does is to open a form on another thread to stop it blocking the main window. It does this as follows

=====
Private sub Click(rowno as int32)
dim vehicle as new dmsvehicle.vehi cle
vehicle.showveh icle(Data(rowno ))
end sub
=====

The Vehicle.showveh icle subroutine is as follows:

====
public sub ShowVehicle(Sto ckNo)
Dim frm As New ViewVehicleStat us(StockNo)

Dim t As New Threading.Threa d(AddressOf frm.dmsShowDial og)

'frm.StockNo = StockNo

t.Start()

end Sub

====

The "dmsShowDia log" subroutine in the form is simply doing a me.showdialog.

This all seems to work fine, and when I close the form I see a message such as "The thread '<No Name>' (0x6ec) has exited with code 0 (0x0)." appear in the output window of the development environment.

However, the no of handles just keeps increasing - I presumed that the garbage collector should tidy up every now and then, but thats not what I'm seeing.

I've not actually duplicated the users issue yet (but I have a high spec machine with masses of memory), but I suspect that this will be the cause of their problem?

Any suggestions as to what I've done wrong?

Thanks in advance

Simon


Nov 21 '05 #4
"Simon Verona" <ne**@aphrodite uk.com> schrieb
I'm deliberately creating the form on the new thread to make the
call to open the form non-blocking. If I didn't create a new
thread, then with the new form open, the original form would block
awaiting it to close.
I did not write you shouldn't create another thread.

What is the reason why you open the Form modally? If you open it modeless
you don't need another thread and you can still work with the previous form.
It is usually not necessary to open Forms in different threads. If there's
work to do that would block the Form(s), you might consider putting only the
work into a different thread.
Am I doing this wrong? Is there an alternative way to do this?
If you want to open it in another thread, I'd also create it there
(quick&dirty):

public sub ShowVehicle(Sto ckNo)
Dim bla As New viewthread
bla.start(stock no)
end Sub

class viewthread
private t as thread
private stockno as stockno

public sub start(StockNo as ?)
me.stockno = stockno
t = new thread(addresso f threadmain)
t.start
end sub

private sub threadmain
Dim frm As New ViewVehicleStat us(me.StockNo)
application.run (frm)
end sub
end class

The class is there to be able to pass data (stockno) to the thread.

I don't use the "dispose" method at all in my code, I presumed that
when the thread died, the garbage collector would tidy up all the
resources used by that thread.. Have I got this wrong?


Yep. Threads, or execution flow in general, has no relation to data and
memory management.

Maybe objects from died threads are collected earlier, but I don't think so.
See also:
http://msdn.microsoft.com/library/en...management.asp

The m.p.d.framework .clr group is probably the best place to ask for details.
Armin

Nov 21 '05 #5
Ok

I open the form modally because if I don't - ie use frm.show then the form
flips open and the immediately disappears again and the thread dies straight
away.

However, application.run (frm) seems to do the same thing as opening it
modally...

I've re-engineered my code to use the format for creating the thread as you
describe. It still uses handles each time I open the form using this
method, though the thread count increases by one as the form opens and then
decreases again when it is closed... I guess that there are some
sub-objects somewhere that it believes are not ready to dispose of, though I
can't think what they might be or where.

I'll try the other newsgroup as you suggest. Thanks for your help.

Regards
Simon
"Armin Zingler" <az*******@free net.de> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
"Simon Verona" <ne**@aphrodite uk.com> schrieb
I'm deliberately creating the form on the new thread to make the
call to open the form non-blocking. If I didn't create a new
thread, then with the new form open, the original form would block
awaiting it to close.


I did not write you shouldn't create another thread.

What is the reason why you open the Form modally? If you open it modeless
you don't need another thread and you can still work with the previous
form. It is usually not necessary to open Forms in different threads. If
there's work to do that would block the Form(s), you might consider
putting only the work into a different thread.
Am I doing this wrong? Is there an alternative way to do this?


If you want to open it in another thread, I'd also create it there
(quick&dirty):

public sub ShowVehicle(Sto ckNo)
Dim bla As New viewthread
bla.start(stock no)
end Sub

class viewthread
private t as thread
private stockno as stockno

public sub start(StockNo as ?)
me.stockno = stockno
t = new thread(addresso f threadmain)
t.start
end sub

private sub threadmain
Dim frm As New ViewVehicleStat us(me.StockNo)
application.run (frm)
end sub
end class

The class is there to be able to pass data (stockno) to the thread.

I don't use the "dispose" method at all in my code, I presumed that
when the thread died, the garbage collector would tidy up all the
resources used by that thread.. Have I got this wrong?


Yep. Threads, or execution flow in general, has no relation to data and
memory management.

Maybe objects from died threads are collected earlier, but I don't think
so. See also:
http://msdn.microsoft.com/library/en...management.asp

The m.p.d.framework .clr group is probably the best place to ask for
details.
Armin

Nov 21 '05 #6
"Simon Verona" <ne**@aphrodite uk.com> schrieb
Ok

I open the form modally because if I don't - ie use frm.show then
the form flips open and the immediately disappears again and the
thread dies straight away.

However, application.run (frm) seems to do the same thing as opening
it modally...
No, because you can still use the other Form becaue they are running in
different threads. Still I am not sure why you open them in different
threads.
I've re-engineered my code to use the format for creating the thread
as you describe. It still uses handles each time I open the form
using this method, though the thread count increases by one as the
form opens and then decreases again when it is closed... I
guess that there are some sub-objects somewhere that it believes are
not ready to dispose of, though I can't think what they might be or
where.
You should examine your code to see which disposable objects you create.
After their usage, they should be disposed. If they created native handles
(those you see in task manager), the handles are destroyed immediatelly. If
you don't call dispose, they will be destroyed also, but later when GC is
done.
I'll try the other newsgroup as you suggest. Thanks for your help.


An attempt:

application.run (frm)
gc.collect
gc.waitforpendi ngfinalizers

just to see what happens. Does it change the behavior you see in the task
manager?

Armin

Nov 21 '05 #7
Simon,
| The symptom that users report is that after they have been using the
| application for a while, it will randomly just crash with an
| exception report (I've not got the details of the error, but
| I'm working on that now!).

You may want to include one or more Global Exception handlers in your
program to log any exception reports from "random" crashes.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.Http Application.Err or event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomai n.UnhandledExce ption event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows. Forms.Applicati on.ThreadExcept ion event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.Thr eadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.Thr eadException handler will catch all
uncaught exceptions from any form/control event handlers.
| What this does is to open a form on another thread to
| stop it blocking the main window. It does this as follows
Why are using Form.ShowDialog on a second thread?

I would simply use Form.Show on the main thread.

Public Sub ShowVehicle(Sto ckNo)

Dim frm As New ViewVehicleStat us(StockNo)

frm.Show()

End Sub

| However, the no of handles just keeps increasing - I presumed that
| the garbage collector should tidy up every now and then,
| but thats not what I'm seeing.

When the user closes a form opened with Show, it will automatically call
Dispose when it is closed. When the user closes a Form opened with
ShowDialog, you need to be certain to call Dispose... Yes the GC will
eventually, at its descrition, call Finalize on the Form, however this may
be long after all the handles have been consumed...

Hope this helps
Jay

"Simon Verona" <ne**@aphrodite uk.com> wrote in message
news:u2******** ******@TK2MSFTN GP12.phx.gbl...
I have a problem in my application which I believe is due to open handles..
..

The symptom that users report is that after they have been using the
application for a while, it will randomly just crash with an exception
report (I've not got the details of the error, but I'm working on that
now!).

I'm trying to reproduce the circumstances by simulating a typical batch of
user tasks on my pc and monitoring whats happening. I'm guessing it's some
problem with threads/handles or something so I've opened up the task manager
and added all the available columns.

What I've noticed is that whilst the application is running, the memory
usage keeps increasing, but more worrying, the no of open handles just keeps
going up and up (it's nearly 4000 handles after running through about 30
tasks).

What the application is doing is as follows:

I have a main form showing, which displays a list from a database.

Then what I'm doing is clicking down the list one by one..

What this does is to open a form on another thread to stop it blocking the
main window. It does this as follows

=====
Private sub Click(rowno as int32)
dim vehicle as new dmsvehicle.vehi cle
vehicle.showveh icle(Data(rowno ))
end sub
=====

The Vehicle.showveh icle subroutine is as follows:

====
public sub ShowVehicle(Sto ckNo)
Dim frm As New ViewVehicleStat us(StockNo)

Dim t As New Threading.Threa d(AddressOf frm.dmsShowDial og)

'frm.StockNo = StockNo

t.Start()

end Sub

====

The "dmsShowDia log" subroutine in the form is simply doing a me.showdialog.

This all seems to work fine, and when I close the form I see a message such
as "The thread '<No Name>' (0x6ec) has exited with code 0 (0x0)." appear in
the output window of the development environment.

However, the no of handles just keeps increasing - I presumed that the
garbage collector should tidy up every now and then, but thats not what I'm
seeing.

I've not actually duplicated the users issue yet (but I have a high spec
machine with masses of memory), but I suspect that this will be the cause of
their problem?

Any suggestions as to what I've done wrong?

Thanks in advance

Simon



Nov 21 '05 #8

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

Similar topics

11
3640
by: Rohit | last post by:
Hi, Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event" handles, by Event handles I mean Win32 Event handles which can be monitored using the ProcessExplorer from www.sysinternals.com, or even simply look at the Handle count in the good old windows TaskManager. To demonstrate the problem, all I did was created a basic Win Forms application and with Main implemented as:
12
1502
by: johnb41 | last post by:
This question is regarding the "Handles" part of a routine, for example: Handles Button1.Click On Form2 (a User Control) I have a button. Here's it's code: Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '...
6
1561
by: antonyliu2002 | last post by:
Right now I put my VB code in the aspx pages. I would prefer to use codebehind, especially many of my VB functions may be shared across multiple aspx pages. I've tried a few pages with codebehind by follwing some examples on the Web and they work just fine. However, I am scared away from codebehind by such extenstions as "Handles MyBase.Load", "Handles Button1.Click" and "Handles MyBase.Init" etc. which we seem to have to add to the...
2
4400
by: sck10 | last post by:
Hello, Is there an equivalent to using "Handles" in the codebehind for c#? Or do I have to declare every event in the GridView control like the following: <asp:GridView ID="gvSearchList" runat="server" DataSourceID="dsSearchList" DataKeyNames="ServiceIdea_ID" OnSelectedIndexChanged="gvSearchList_SelectedIndexChanged">
0
8966
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9257
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6749
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6052
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3278
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.