473,469 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Disposing Pattern

Hi,

Could someone explain me if I need to implement the Dispose pattern on
managed objects that hold objects which have longer life or is a
singleton?

Example -
Class A holds Class Global where Class Global is a singleton.

So should Class A have
private Global g;
#region IDisposable Members

public void Dispose() {
// TODO: Add TagDataIOExecutor.Dispose implementation
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing) {
g = null;
}

~A() {
Dispose(false);
}

#endregion
-Thanks
mb

Nov 17 '05 #1
4 1373
Hi mb,

Even if Global implements singleton pattern, setting "g = null" does
not mean you dispose it. It just means g will mo longer reference that
singleton's memory location. Did you omit a line like: g.Dispose() ? If
no, your class does not hold resources that need disposing, you don't
need to consider the dispose pattern at all.
Could someone explain me if I need to implement the Dispose pattern on
managed objects that hold objects which have longer life or is a
singleton?

Generally, a class should not dispose object if it does not control the
object's life time.

Regards,
Thi

Nov 17 '05 #2
Thank you for the reply.

Its true 'a' an instance of A does not control the life span of 'g'.
But how would GC determine that 'a' is no longer in use?

Nov 17 '05 #3
su***************@gmail.com wrote:
Thank you for the reply.

Its true 'a' an instance of A does not control the life span of 'g'.
But how would GC determine that 'a' is no longer in use?


The GC walks the forest of "reachable" objects, taking its "roots" from
static variables and local variables in all the stack frames in all
threads. It starts out with the assumption that everything can be
thrown away, and marks those objects which it finds can't be thrown
away.

It's more complex than that, partly due to generations, but that's the
basic thrust of it.
See http://msdn.microsoft.com/msdnmag/is...I/default.aspx for
more details.

Jon

Nov 17 '05 #4
Thanks you for the explanation. I tried a sample example and did
observe the same behaviour.

Nov 17 '05 #5

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

Similar topics

1
by: Richard Morris | last post by:
Hi, I was wondering if someone might be able to shed some light on a problem that I have. This application that I am writing has several JInternalFrames but some of them can only have one...
10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
5
by: Dennis | last post by:
When I instantiate a Brush resource such as a solidbrush, do I need to dispose the resource when I'm finished or will it automatically be disposed when the function/sub it's used in ends? Thank...
5
by: Chris | last post by:
I have a form that requires drawing custom lines on it. The color of the lines is suppose to be the same as the forcolor of the form. Am I doing this the most efficent and correct way? ...
10
by: sternr | last post by:
Hey, I have a C# assembly that inherits from ServicedComponent and is installed in the Com+ with ServerActivation. And I have a C++ application that calls this Com+ object successfully, But the...
4
by: AAJ | last post by:
Hi I have written a wrapper that encapsulates the Excel object model. As anyone who's done a similar thing knows, its important to release the objects when the app exits otherwise they still...
4
by: Peter Webb | last post by:
I am supposed to manually dispose of some instances, such as Brushes, right? I have a couple of questions: 1. I have the following code, and it works just fine: ...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
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...
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
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,...
1
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.