Dispose and Finalize

  • Thread starter Klaus Löffelmann
  • 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
 
K

Klaus Löffelmann

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
 

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

Top