Is this a bad code?

  • Thread starter Thread starter Zeng
  • Start date Start date
Z

Zeng

I remember reading that accessing member variables that are managed objects
in the Finalizer is not safe, if that is true, then this code is bad right?
Thanks!

internal class CMyClass
{
ArrayList m_array = new ArrayList( 50000 );
private string m_idStr;
private Guid m_guid;
internal CMyClass( int n )
{
for( int i = 0; i < n; ++i )
m_array.Add( Guid.NewGuid() );

m_guid = Guid.NewGuid();
m_idStr = m_guid.ToString();
}

internal Guid id
{
get{ return (Guid ) m_array[ 0 ]; }
}
~CMyClass()
{
Trace.Write( m_guid, m_idStr );
}
 
Zeng said:
I remember reading that accessing member variables that are managed objects
in the Finalizer is not safe, if that is true, then this code is bad right?

No. The problem with accessing managed objects in a finalizer is that
they may have been finalized already - so trying to flush a stream in a
finalizer is a bad idea, for instance.

Using a string and a Guid, however, is fine - neither of those have
finalizers.
 
hi

beside of skeet's comments, What do you want to do?

It seems you are always returning the Guid at position 0


cheers,
 
It was written to point out the situation, it wasn't supposed to do anything
useful. Thanks for looking into this

Ignacio Machin ( .NET/ C# MVP ) said:
hi

beside of skeet's comments, What do you want to do?

It seems you are always returning the Guid at position 0


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Zeng said:
I remember reading that accessing member variables that are managed objects
in the Finalizer is not safe, if that is true, then this code is bad
right?
Thanks!

internal class CMyClass
{
ArrayList m_array = new ArrayList( 50000 );
private string m_idStr;
private Guid m_guid;
internal CMyClass( int n )
{
for( int i = 0; i < n; ++i )
m_array.Add( Guid.NewGuid() );

m_guid = Guid.NewGuid();
m_idStr = m_guid.ToString();
}

internal Guid id
{
get{ return (Guid ) m_array[ 0 ]; }
}
~CMyClass()
{
Trace.Write( m_guid, m_idStr );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top