473,399 Members | 4,254 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,399 software developers and data experts.

safe to use string member class in finalizer?

Zen
Hi,

Is my finalizer safe? Thanks!!

public class MyClass
{
private string m_str;

public MyClass( string s )
{
m_str = s;
}

~MyClass()
{
// is this safe?
if( m_str == "blah" )
// do something ;
}
}

May 8 '06 #1
4 1797
Hi,

The finalizer is intended to be used to cleanup unmanaged resources, these
resources need to be kept referenced in the class, in short, yes, you can
use the member variables inside the finalizer.

I suggest you to take a look at the IDisposable interface though.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Zen" <ze*@nononospam.com> wrote in message
news:eE****************@TK2MSFTNGP02.phx.gbl...
Hi,

Is my finalizer safe? Thanks!!

public class MyClass
{
private string m_str;

public MyClass( string s )
{
m_str = s;
}

~MyClass()
{
// is this safe?
if( m_str == "blah" )
// do something ;
}
}

May 8 '06 #2
Zen
Thanks for replying but your provided reason somehow doesn't convince me
that it's the case for strings. I thought it was that simple too until I
read this part of http://msdn.microsoft.com/msdnmag/issues/1100/gci/: The
runtime doesn't make any guarantees as to the order in which Finalize
methods are called. For example, let's say there is an object that contains
a pointer to an inner object. The garbage collector has detected that both
objects are garbage. Furthermore, say that the inner object's Finalize
method gets called first. Now, the outer object's Finalize method is allowed
to access the inner object and call methods on it, but the inner object has
been finalized and the results may be unpredictable. For this reason, it is
strongly recommended that Finalize methods not access any inner, member
objects.

Any other comments and suggestions? thanks!
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Ou**************@TK2MSFTNGP02.phx.gbl...
Hi,

The finalizer is intended to be used to cleanup unmanaged resources, these
resources need to be kept referenced in the class, in short, yes, you can
use the member variables inside the finalizer.

I suggest you to take a look at the IDisposable interface though.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Zen" <ze*@nononospam.com> wrote in message
news:eE****************@TK2MSFTNGP02.phx.gbl...
Hi,

Is my finalizer safe? Thanks!!

public class MyClass
{
private string m_str;

public MyClass( string s )
{
m_str = s;
}

~MyClass()
{
// is this safe?
if( m_str == "blah" )
// do something ;
}
}


May 8 '06 #3
Thanks for replying but your provided reason somehow doesn't convince me
that it's the case for strings. I thought it was that simple too until I
read this part of http://msdn.microsoft.com/msdnmag/issues/1100/gci/: The
runtime doesn't make any guarantees as to the order in which Finalize
methods are called.


Right, but String doesn't have a finalizer since it doesn't wrap any
unmanaged resources. Therefore it's safe to use.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 8 '06 #4
Hi,

"Zen" <ze*@nononospam.com> wrote in message
news:eE**************@TK2MSFTNGP05.phx.gbl...
Thanks for replying but your provided reason somehow doesn't convince me
that it's the case for strings. I thought it was that simple too until I
read this part of http://msdn.microsoft.com/msdnmag/issues/1100/gci/: The
runtime doesn't make any guarantees as to the order in which Finalize
methods are called. For example, let's say there is an object that
contains a pointer to an inner object. The garbage collector has detected
that both objects are garbage. Furthermore, say that the inner object's
Finalize method gets called first. Now, the outer object's Finalize method
is allowed to access the inner object and call methods on it, but the
inner object has been finalized and the results may be unpredictable. For
this reason, it is strongly recommended that Finalize methods not access
any inner, member objects.

Any other comments and suggestions? thanks!


I guess that is part of the reason why the IDisposable was created :)

btw, string are safe to use, they contain no unmanaged resources.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
May 10 '06 #5

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

Similar topics

13
by: Emmanuel | last post by:
Hi, I run across this problem, and couldn't find any solution (python 2.2.2) : Code : =========== from __future__ import generators >>> class titi:
1
by: Timothy Madden | last post by:
I want to stoare in a queue values of type LPCTSTR, int, long, BYTE, WORD, DWORD and many enum types. Can I write such a queue in a type safe maner (idealy no casts involved) ? Thank you...
12
by: Vaca Louca | last post by:
Hello, I write an ISAPI authentication module which uses Berkeley DB and want it to be as efficient as possible. Both ISAPI and BerkeleyDB use arrays of chars (char *) to pass and receive...
1
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
18
by: digz | last post by:
Hi , I am trying to write a class that encapsulates a Union within it , I intend the code to be exception safe. can someone please review it and tell me if I have done the right thing , are...
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
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,...
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...
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...

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.