473,473 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dispose and Finalize

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()
Has been called by the GC! At this point I'm not sure, if I can safely use the FileStream-Object,
since it could already be finalized, couldn't it?!


Else
if

End If
End If


End Sub
Nov 20 '05 #1
1 958
Started new discussion in CLR-Newsgroup.

Klaus

"Klaus Löffelmann" <fo***********@loeffelmann.de> schrieb im Newsbeitrag
news:Ou**************@tk2msftngp13.phx.gbl...
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()
Has been called by the GC! At this point I'm not sure, if I can safely use the FileStream-Object,
since it could already be finalized, couldn't it?!


Else
if

End If
End If


End Sub

Nov 20 '05 #2

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

Similar topics

4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
4
by: Sunit Joshi | last post by:
Hello All I have an abstract class C1 with this: public abstract class C1 { protected bool m_Dirty; protected override void Dispose(bool disposing) { if(m_Dirty) WriteOuput();
4
by: Joe Abou Jaoude | last post by:
I m preparing to pass the 70-306 exam, so i downloaded Q & A from multiple sites. There's this question that really confuses me, coz i see that both answers A and C are both correct. Can anyone...
6
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
3
by: Boni | last post by:
Dear all, can somebody explain difference between Dispose and Finalize and when each of them should be used? Thank you very much, Boni
2
by: eBob.com | last post by:
I have a user control which creates an Excel spread sheet and badly needs Dispose/Finalize. I've read up on the subject in Balena and I think I understand what is going on. But the VS generated...
6
by: Water Cooler v2 | last post by:
I heard from someone that we must not implement IDisposable for all classes. Can someone please tell me: 1. the reason why we must not implement IDisposable for all the classes we write. 2....
4
by: BLUE | last post by:
I've read many articles including the one from Duff's blog but I've many doubts. public static myClass Instance { get { if (myClass.instance == null) myClass.instance = new myClass();
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.