Dispose and Finalize

  • Thread starter Thread starter Klaus Löffelmann
  • Start date Start date
K

Klaus Löffelmann

Hello,



If I have an object which is about to become finalized, and this object has
instantiated several other objects during its lifetime, how can I be sure
(or find out) that those objects haven't been killed by the GC, already?



Take, for example, a class that uses a FileStream-object which can be open
during the object's lifetime.

A dispose-method for it may look somewhat like this:



'Called by Me.Dispose() or Me.Finalize()

Public Sub Dispose(ByVal Disposing As Boolean)



'Hasn't been called by GC
If Disposing Then



'Already disposed?
If myDisposed Then
Dim up As New ObjectDisposedException("WhatEverObject")
Throw up
End If



'Some Property to find out what to do

If mySerializerMode = SoapSerializerMode.OpenForWriting Then
myFileStream.Flush()
End If
myFileStream.Close()
FileStream-Object,

Else
if



End If
End If




End Sub
 
Started new discussion in CLR-Newsgroup.

Klaus

Klaus Löffelmann said:
Hello,



If I have an object which is about to become finalized, and this object has
instantiated several other objects during its lifetime, how can I be sure
(or find out) that those objects haven't been killed by the GC, already?



Take, for example, a class that uses a FileStream-object which can be open
during the object's lifetime.

A dispose-method for it may look somewhat like this:



'Called by Me.Dispose() or Me.Finalize()

Public Sub Dispose(ByVal Disposing As Boolean)



'Hasn't been called by GC
If Disposing Then



'Already disposed?
If myDisposed Then
Dim up As New ObjectDisposedException("WhatEverObject")
Throw up
End If



'Some Property to find out what to do

If mySerializerMode = SoapSerializerMode.OpenForWriting Then
myFileStream.Flush()
End If
myFileStream.Close()

Else
if



End If
End If




End Sub
 
Back
Top