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

Disposing of a static object

Ive a static instance of an object in a class. It needs to be Dispose(d) of
when it's finished with.
How do I do this? Or don't I have to worry about it?
Class myClass
{
private static TMyClass myClass = null;

public static bool ClassConnected
{
if (myClass == null) myClass = new(TMyClass);
return myClass.Connected;
}
}
Nov 17 '05 #1
3 9260
Claire,

If you disposed of the instance myClass, then you would have to create a
new instance every time the ClassConnected property is called.

The lifetime of the object assigned to myClass is up to you, but
generally, I don't think that getting rid of the static instance is going to
do you any good.

What is it that you are trying to do exactly?

BTW, the line

if (myClass == null) myClass = new(TMyClass);

Is incorrect, it should be:

if (myClass == null) myClass = new TMyClass();

Assuming it has a parameterless constructor.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Claire" <as*******@ntlworld.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ive a static instance of an object in a class. It needs to be Dispose(d)
of when it's finished with.
How do I do this? Or don't I have to worry about it?
Class myClass
{
private static TMyClass myClass = null;

public static bool ClassConnected
{
if (myClass == null) myClass = new(TMyClass);
return myClass.Connected;
}
}

Nov 17 '05 #2
Hi Nicholas
Pardon my poor pseudocode, I noticed that after I posted.
I can't find the real code now, but it worried me in a general hunt for
memory leaks/efficiency whether I should worry about disposal of static live
instances
(memory profilers are difficult to understand at first)
Nov 17 '05 #3
Claire wrote:
Ive a static instance of an object in a class. It needs to be Dispose(d) of
when it's finished with.
Invoke Dispose() on it when you are done using it :)

Is your problem that it's unclear when that is? If this is the case, you
probably can't get by with having a static instace.
How do I do this? Or don't I have to worry about it?


Doesn't "needs to be Dispose(d) of", mean that you have to worry about it?

You don't need to Dispose object just for the sake of memory or closing
files, although you need to Flush() streams if the aren't closed or
disposed.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #4

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

Similar topics

5
by: Buddy Ackerman | last post by:
I have taken over an application that has a sealed (singleton) class for database access. I want to add a private SQLConnection class variable and open the connection it whenever it is instantiated...
15
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
7
by: Bryan D. | last post by:
Our application makes extensive use of C#'s event handling mechanism to communicate between classes. A problem has cropped in that it becomes difficult to know for sure that all observers of an...
4
by: MC D | last post by:
Question: If I have a class which has a property which is a collection of widget objects (an arrayList of widgets), and both the containter class and the widget class implement the IDisposable...
4
by: Dakkar | last post by:
I have a program with windows forms and after execution of my program im making it invisible for working background progress and i have a dispose function like this protected override void...
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: 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? ...
8
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...

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.