473,411 Members | 1,889 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,411 software developers and data experts.

Code in finalize causes session to end!

Rob
This is a weird one... I've got a class called PageInfo that has the
following finalize code:

Protected Overrides Sub Finalize()
MyBase.Finalize()
Do While m_TempFolders.Count
Dim TempPath As String = m_TempFolders.Dequeue
Granite.DeleteFolder(TempPath)
Loop
End Sub

The Granite.DeleteFolder routine is show belown. Basically, the class uses
some temporary folders. When the class creates them, it sticks the path in
the m_TempFolders queue. When the class is destroyed by the garbage
collector, the Finalize routine is called and deletes the temporary folder.
The way the program works is that upto ten PageInfo objects are kept in a
stack and when the 10th one is added, the one out of the other end of the
stack is deleted. This causes the finalize routine to be called shortly
afterwards.

The temporary folder is getting deleted okay BUT the session terminates -
the ASP.NET v2 Session_End event first, followed shortly by the
Session_Start.

Is it legal to call code like in DeleteFolder from within Finalize?

Thanks, Rob.

' DeleteFolder: Removes a folder and *all* the contents. USE WITH CARE!!

Public Sub DeleteFolder(ByVal Path As String)
If System.IO.Directory.Exists(Path) Then
For Each File As String In System.IO.Directory.GetFiles(Path)
Try
System.IO.File.SetAttributes(File,
IO.FileAttributes.Normal)
System.IO.File.Delete(File)
Catch ex As Exception
' Ignore delete error
End Try
Next
For Each Directory As String In
System.IO.Directory.GetDirectories(Path)
DeleteFolder(Directory)
Next
Try
System.IO.Directory.Delete(Path)
Catch ex As Exception
' Ignore delete folder error
End Try
End If
End Sub
Dec 11 '07 #1
8 1323
Rob
Protected Overrides Sub Finalize()
MyBase.Finalize()
Do While m_TempFolders.Count
Dim TempPath As String = m_TempFolders.Dequeue
Granite.DeleteFolder(TempPath)
Loop
End Sub
PS. If I comment out the Granite.DeleteFolder call, then the problem doesn't
occur - except of course the temp folder isn't deleted.

Rob.
Dec 12 '07 #2
Rob,

Why are you using the finalize method to do normal things. It is advices to
avoid the finalize as hell to let the managed code to work properly. Don't
this not give problems?

Cor

Dec 12 '07 #3
Rob
Why are you using the finalize method to do normal things. It is advices
to avoid the finalize as hell to let the managed code to work properly.
Don't this not give problems?
The Finalize() method is the only way to ensure that the resources
(temporary folders) in this case are cleaned up. These PageInfo objects hold
(on the server) information about pages a user has previously visited. The
user could at any time go back to these pages. There could equally just
close the browser window. The application has no idea this has happened.
Therefore, the normal "cleanup" trigger in a Windows program doesn't occur.
All you get is the session ending (say) 20 minutes later.

I suppose I could change the code to delete the folders in the Session_End()
event but that kinds of annoys as the cleanup of the resources doesn't
belong with the objects it's associated with anymore.

The question as to why deleting folders and files from Finalize() causes a
new session is still confusing though. I did read somewhere that the
Finalize() method may be called from a different thread to the rest of the
application which may be important...

Cheers, Rob.
Dec 12 '07 #4
Folders are monitored by the ASP.NET application so if you change a folder
beneath your web application you might cause an application restart (if 2.0
you can use health monitoring to easily track those restarts).

My personal preference would be to put my temp files outside of my web site.

--
Patrice

"Rob" <rob_nicholson@nospam_unforgettable.coma écrit dans le message de
news: 3_*****************@newsfe3-win.ntli.net...
>Why are you using the finalize method to do normal things. It is advices
to avoid the finalize as hell to let the managed code to work properly.
Don't this not give problems?

The Finalize() method is the only way to ensure that the resources
(temporary folders) in this case are cleaned up. These PageInfo objects
hold (on the server) information about pages a user has previously
visited. The user could at any time go back to these pages. There could
equally just close the browser window. The application has no idea this
has happened. Therefore, the normal "cleanup" trigger in a Windows program
doesn't occur. All you get is the session ending (say) 20 minutes later.

I suppose I could change the code to delete the folders in the
Session_End() event but that kinds of annoys as the cleanup of the
resources doesn't belong with the objects it's associated with anymore.

The question as to why deleting folders and files from Finalize() causes a
new session is still confusing though. I did read somewhere that the
Finalize() method may be called from a different thread to the rest of the
application which may be important...

Cheers, Rob.

Dec 12 '07 #5
On Tue, 11 Dec 2007 23:52:41 GMT, "Rob"
<rob_nicholson@nospam_unforgettable.comwrote:
>This is a weird one... I've got a class called PageInfo that has the
following finalize code:

Protected Overrides Sub Finalize()
MyBase.Finalize()
Do While m_TempFolders.Count
Dim TempPath As String = m_TempFolders.Dequeue
Granite.DeleteFolder(TempPath)
Loop
End Sub

The Granite.DeleteFolder routine is show belown. Basically, the class uses
some temporary folders. When the class creates them, it sticks the path in
the m_TempFolders queue. When the class is destroyed by the garbage
collector, the Finalize routine is called and deletes the temporary folder.
The way the program works is that upto ten PageInfo objects are kept in a
stack and when the 10th one is added, the one out of the other end of the
stack is deleted. This causes the finalize routine to be called shortly
afterwards.

The temporary folder is getting deleted okay BUT the session terminates -
the ASP.NET v2 Session_End event first, followed shortly by the
Session_Start.

Is it legal to call code like in DeleteFolder from within Finalize?
Why not implement IDisposable, and call yourObject.Dispose() when you
reach 10 items?

Finalizers should only be used for when *directly* handling unmanaged
resources, which you are not doing here. (Unmanaged = IntPtr,
usually; managed = FileStream, etc)

Mach
Dec 12 '07 #6
Rob
Folders are monitored by the ASP.NET application so if you change a folder
beneath your web application you might cause an application restart (if
2.0 you can use health monitoring to easily track those restarts).
Ahh - that might explain a lot... I can probably record it to not use a
temporary folder at all and simply put the files in a single flat temp
folder.
My personal preference would be to put my temp files outside of my web
site.
The temp folders contain temporary HTML files that have to within the
website as they are served up to the browser. The particular application is
a pubs database where there are guide entries per pub. The user can be
working on a pub entry and request to display how the guide entry will look.
This requires a temporary HTML and JPG to be generated, displayed and then
thrown away.

Cheers, Rob.
Dec 13 '07 #7
Rob
Why not implement IDisposable, and call yourObject.Dispose() when you
reach 10 items?
Because I kind of like not having to worry about cleanup in VB.NET if you
see what I mean. I maintain a large VB6 application and making sure that we
cleanup resources manually is one of the biggest headaches of the entire
system.

I assumed that unless you were worried about scarse resources then relying
upon the garbage collector was "good practise" and that IDispose should be
used for those cases where you want to instantly free up resources and not
depending upon the garbage collector doing it at some undermined point in
the future.

The problem I see with IDispose is that it has to be called manually by the
using code which is IMHO no better than having to implement memory cleanup
in C++ destructors.

Finalize is attractive in that you know that it's going to be called
*automatically* by the garbage collector at some point so you don't have to
worry about manually calling the dispose method.
Finalizers should only be used for when *directly* handling unmanaged
resources, which you are not doing here. (Unmanaged = IntPtr,
usually; managed = FileStream, etc)
Can you make temporary files managed?

Actually, I'm tempted to maintain a list of temporary files to be deleted in
the session state and delete them in one go when the session ends.

I suspect the other post about the application restarting when folders
change in a website is probably the cause.

Cheers, Rob.
Dec 13 '07 #8
Rob
Folders are monitored by the ASP.NET application so if you change a folder
beneath your web application you might cause an application restart (if
2.0 you can use health monitoring to easily track those restarts).
You are exactly spot on here!! I happened to have a break point in
Session_End and manually deleted a folder in the ~/Temp/ folder and bang -
it fired.

I've recoded the temporary files to just use files and not folders.

Cheers, Rob.
Dec 13 '07 #9

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

Similar topics

2
by: Jack | last post by:
The following is the error: Error Type: Microsoft JET Database Engine (0x80040E14) Syntax error (missing operator) in query expression 'ApplicantIntID ='. /subgrantapptest/ListSaved.asp, line...
10
by: Anthony Williams | last post by:
Hi gang, This one looks like a bug :o( As you may or may not know, setting session management in web.config to use cookieless sessions causes the ASP.NET runtime to munge a session ID into...
2
by: Ricky K. Rasmussen | last post by:
Hi NG, We have a rather large ASP.NET application that uses popups to display various dialogs to the user. In our work we've come over a rather annoying "bug": If we open a modal dialog using...
20
by: Charles Law | last post by:
I have an application that creates a class. The class has unmanaged resources, so must end gracefully. How can I guarantee that the unmanaged resources are freed? I have looked at IDisposable,...
3
by: Boni | last post by:
Dear all, can somebody explain difference between Dispose and Finalize and when each of them should be used? Thank you very much, Boni
6
by: Andy Sutorius via DotNetMonster.com | last post by:
Using the code below the browser just sits and spins. The dll is located in the root of the web app. System.Runtime.Interop is in the using statements. I have tried this in ASP.NET 1.1 and 2.0 and...
5
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
7
by: SalamElias | last post by:
I started to use the Finalize sub in aspax page, I put a break point on one of the lines, hit run with debug, the pages dispalys without breaking on the line. 2nd question, do we need to use the...
8
by: Rob | last post by:
This is a weird one... I've got a class called PageInfo that has the following finalize code: Protected Overrides Sub Finalize() MyBase.Finalize() Do While m_TempFolders.Count Dim TempPath As...
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: 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...
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
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...
0
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...

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.