473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dispose pattern in ASP.NET pages

Hi,
I'm using managed wrappers that contain unmanaged code in my web pages.
As I have some problems with memory (the memory grows and is not
returned to the OS) though I delete unmanaged c++ objects in destructors
I want to explicitly destroy objects using Dispose. But the problem is
when to call it? There is no event telling the user is leaving a page to
put in it code which disposes the objects.
Thanks in advance.

Regrads,
Mircu
Nov 18 '05 #1
9 4660
Hi Mircu:

Your ASPX page does not stay 'running' while the client displays the
page. As soon as the response is complete the runtime is free to clean
everything up associated with the request, including the page object
itself. Even if the client were to somehow notify the server that it
is navigating away from a page, your ASPX page that processed the
request would be long gone.

Manish has some good advice, which is create and dispose the object
with resources as soon as possible.

If the object has to be around for several events (i.e. if you would
use it from Page_Load and then some Button click events), you could
always hook the Unload event of the Page and call Dispose on your
object from there.

HTH,

--
Scott
http://www.OdeToCode.com

On Fri, 14 May 2004 13:19:49 +0200, mircu <mi***@op.pl> wrote:
Hi,
I'm using managed wrappers that contain unmanaged code in my web pages.
As I have some problems with memory (the memory grows and is not
returned to the OS) though I delete unmanaged c++ objects in destructors
I want to explicitly destroy objects using Dispose. But the problem is
when to call it? There is no event telling the user is leaving a page to
put in it code which disposes the objects.
Thanks in advance.

Regrads,
Mircu


Nov 18 '05 #2
Użytkownik Scott Allen napisał:
Your ASPX page does not stay 'running' while the client displays the
page. As soon as the response is complete the runtime is free to clean
everything up associated with the request, including the page object
itself. Even if the client were to somehow notify the server that it
is navigating away from a page, your ASPX page that processed the
request would be long gone.


I knew that after displaying the page the processing is completed on the
server. But as you pointed out, my object must live between postbacks.
When exactly the Unload event is called? If it is as you say it'd be
fine to dispose objects there.

Regrads,
Mircu
Nov 18 '05 #3
Użytkownik Manish Jadhav napisał:
I am assuming that's your's is a web application. In that case it is always better to create the object, use it, display whatever information you need to (on your web page) and immediately dispose off the object.


Yes, you're right its a web application. I'd like to dispose objects as
early as possible but if the object must live between postbacs (eg. it's
bound to a datagrid) it isn't clear when it will be disposed.

Regards,
Mircu
Nov 18 '05 #4
Hi Mircu,

If you have to keep the object around then Unload will not be of help.
Unload will happen after request processing completes. So how are you
keeping a reference around between postbacks? Keeping it in the
Session or Cache?

--s

On Fri, 14 May 2004 20:34:20 +0200, mircu <mi***@op.pl> wrote:
Użytkownik Scott Allen napisał:
Your ASPX page does not stay 'running' while the client displays the
page. As soon as the response is complete the runtime is free to clean
everything up associated with the request, including the page object
itself. Even if the client were to somehow notify the server that it
is navigating away from a page, your ASPX page that processed the
request would be long gone.


I knew that after displaying the page the processing is completed on the
server. But as you pointed out, my object must live between postbacks.
When exactly the Unload event is called? If it is as you say it'd be
fine to dispose objects there.

Regrads,
Mircu


--
Scott
http://www.OdeToCode.com
Nov 18 '05 #5
Użytkownik Scott Allen napisał:
If you have to keep the object around then Unload will not be of help.
Unload will happen after request processing completes. So how are you
keeping a reference around between postbacks? Keeping it in the
Session or Cache?


I keep them in the Session and when the user exits the page clicking
'Close' button or others I remove it from the Session if necessary. Hmm,
should it help if I call Dispose before removing from the session?

BTW I know that storing objects in the Session isn't good. I'm thinking
on implementing automatic object removing from the Session. The plan is
to have a structure to remember what objects are needed in the session
for specified page, and on every request test what page is called and
then remove unnecessary objects from the Session.

Regards,
Mircu
Nov 18 '05 #6
"mircu" <mi***@op.pl> wrote in message
news:en**************@TK2MSFTNGP09.phx.gbl...
Użytkownik Scott Allen napisał:
If you have to keep the object around then Unload will not be of help.
Unload will happen after request processing completes. So how are you
keeping a reference around between postbacks? Keeping it in the
Session or Cache?
I keep them in the Session and when the user exits the page clicking
'Close' button or others I remove it from the Session if necessary. Hmm,
should it help if I call Dispose before removing from the session?


I'd call Dispose after removing it from Session.
BTW I know that storing objects in the Session isn't good.
This is ASP.NET, not ASP. In general, it's perfectly fine to store objects
in Session. I'm thinking
on implementing automatic object removing from the Session. The plan is
to have a structure to remember what objects are needed in the session
for specified page, and on every request test what page is called and
then remove unnecessary objects from the Session.


This is very error-prone. A program shouldn't have to know details of how it
is programmed.

Instead, how about handing Session_End in global.asax, removing everything
from Session, and testing each one to see if it implements IDisposable. If
it does, call its Dispose method.
--
John Saunders
John.Saunders at SurfControl.com

Nov 18 '05 #7
Użytkownik John Saunders napisał:
Instead, how about handing Session_End in global.asax, removing everything
from Session, and testing each one to see if it implements IDisposable. If
it does, call its Dispose method.


Most of the objects I put in the Session are used only in one page and
they are accessed in postbacks. In that case removing objects in
Session_End is too late because it would be fired not before the user
session ends. But your idea - testing if IDisposable is implemented and
then calling Dispose is nice. Thanks.

Regards,
Mircu
Nov 18 '05 #8
"mircu" <mi***@op.pl> wrote in message
news:eD**************@TK2MSFTNGP12.phx.gbl...
Użytkownik John Saunders napisał:
Instead, how about handing Session_End in global.asax, removing everything from Session, and testing each one to see if it implements IDisposable. If it does, call its Dispose method.


Most of the objects I put in the Session are used only in one page and
they are accessed in postbacks. In that case removing objects in
Session_End is too late because it would be fired not before the user
session ends. But your idea - testing if IDisposable is implemented and
then calling Dispose is nice. Thanks.


If your page knows when there will no longer be any postbacks to it, then
you could remove the items when your page determines that it is done.

I'm concerned about what would happen if the user used the Back button in
his browser. Your page could be in the middle of a sequence of postbacks,
expecting to find Session state, only it's been removed. You may have to
deal with that situation.
--
John Saunders
John.Saunders at SurfControl.com

Nov 18 '05 #9
Użytkownik John Saunders napisał:
If your page knows when there will no longer be any postbacks to it, then
you could remove the items when your page determines that it is done.
When the user clicks only buttons it's ok I can determine if it's done.
Worse is when the user exits page by specifying new url in the address bar.
I'm concerned about what would happen if the user used the Back button in
his browser. Your page could be in the middle of a sequence of postbacks,
expecting to find Session state, only it's been removed. You may have to
deal with that situation.


Yes, it's a problem and therefore I think of implementing someting to
automatically clean Session.

Regards,
Mircu
Nov 18 '05 #10

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...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
4
by: Sunit Joshi | last post by:
Hello All I have an abstract class C1 with this: public abstract class C1 { protected bool m_Dirty; protected override void Dispose(bool disposing) { if(m_Dirty) WriteOuput();
7
by: Tamir Khason | last post by:
I have a class public class Foo { public Foo() { DoSomething() } } I want to be able to do something else while the class enters GC - no more
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
4
by: Sharon | last post by:
Hi. I put a message box in the form designer, Dispose method. Clicking the form close (X) button, i noticed that the message, appears twice. My code does not call Dispose. What is happening...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
5
by: Markus Stoeger | last post by:
Hi, I have a class similar to that: class MyClass : IDisposable { IDisposable obj1; IDisposable obj2; IDisposable obj3; MyClass() {
3
by: AlexS | last post by:
When I implement Dispose pattern in object implementing IDisposable, current fxcop recommends: Ensure that Wrapper.Dispose():Void is declared as public and sealed. However, if I do as it asks,...
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
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,...
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...
1
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,...
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.