I have seen  people use 2 phase dispose ...

ie  in the implimentation of IDisposable  if ( cleanup_resource )

base.Dispose()  // or InternalDispose
this is often done in situations where you get multiple disposes ( since
disposing var is not that reliable) , but you can disarm it as well as you
have done.

Also exceptions during dispose can be painful , which happen more often
than you think.

Why not use try / finaly  for more control  - using becomes try finally {
Dispose() }  anyway in CIL ..   eg the following calls dispose immediately
on exception but if there is no error dispose is only called when the GC
collects the object and it means you dont have fancy dispose code ( which
is hard to spot) .

class Parser {
  ParseSomeNonTerminal(...)
  {
     var completed = false;
     var marker = new InputMarker(tokenStream))
     try{
         foreach ( var thing in marker.Things )
              thing.Parse();
         var result = new SomeAST(first, second);
         completed = true;
         return result;
           }
            finaly {

                if ( result == false)
                {
              m_tokenPos = marker.tokenPos;
                       marker.Dispose();
               }
              //FIXME do we need to dispose on success ?

           }
  }
}

This code can have issues and may not clean up resources eg  when there is
no error and tokenstream is disposed before  InputMarker by the GC .
InputMarker then tries to dispose tokenstream and fails with a null ref
exception and may fail to clean up an unmanaged resource  (which is not
really seen as its done by the GC and ignored - this is why its useful in a
debuger having all throw exceptions trapped and not too many exceptions
generated)
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev

Reply via email to