Advertisement

09.16.2007 at 11:05PM PDT, ID: 22832481
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

object destruction
Tags: destruction, destroy, force
Hi

im trying to use the finalize method to destroy an object , i cant seem to get the method to run since the console never
writes the line that the dog object is destroyed!

also according to the msdn net 1.1
http://msdn2.microsoft.com/en-us/library/hks5e2k6(vs.71).aspx

The Finalize destructor is a protected method that can be called only from the class it belongs to, or from derived classes. The system calls Finalize automatically when an object is destroyed, so you should not explicitly call Finalize from outside of a derived class's Finalize implementation.

i would though like to know how i can the finalize method explicit ? to force destruction of an object!


Public Class Dog

    'The age variable

    Private Age As Integer


    'This is the destructor:

    Protected Overrides Sub Finalize()

        Console.WriteLine("Dog is Destroyed")

    End Sub

    'The default constructor:

    Public Sub New()

        Console.WriteLine("Dog is Created With Age Zero")

        Age = 0

    End Sub

    'The parameterized constructor:

    Public Sub New(ByVal val As Integer)

        Console.WriteLine("Dog is Created With Age " + Convert.ToString(val))

        Age = val

    End Sub

    'The Main Function

    Shared Sub Main()


        Dim Jimmy, Jacky As Dog

        'Create the objects

        'This will call the default constructor

        Jimmy = New Dog

        'This will call the parameterized constructor

        Jacky = New Dog(10)

    End Sub

End Class

thanks
vbturbo
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: vbturbo
Solution Provided By: AlexFM
Participating Experts: 3
Solution Grade: A
Views: 5
Translate:
Loading Advertisement...
09.16.2007 at 11:58PM PDT, ID: 19903265

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.17.2007 at 02:41AM PDT, ID: 19903709

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.17.2007 at 04:02AM PDT, ID: 19903958

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.17.2007 at 04:57AM PDT, ID: 19904179

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.17.2007 at 05:30AM PDT, ID: 19904296

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.17.2007 at 05:32AM PDT, ID: 19904313

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.17.2007 at 05:44AM PDT, ID: 19904376

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.17.2007 at 08:45PM PDT, ID: 19910123

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.18.2007 at 03:16AM PDT, ID: 19911477

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
09.16.2007 at 11:58PM PDT, ID: 19903265

Rank: Sage

Set all variables to Nothing and add these lines to the end of the Main function:

GC.Collect()
GC.WaitForPendingFinalizers()
 
09.17.2007 at 02:41AM PDT, ID: 19903709

Rank: Sage

Finalize is non-deterministic destruction - it is called by the GC, so you can never be sure when it's going to get called (calling the GC manually as AlexFM suggested uses a lot of resources and is generally not advised).

Dispose() is deterministic destruction - you explicitly call it.

You can read much more e.g. in the following articles:

http://msdn2.microsoft.com/en-us/library/b1yfkh5e(vs.71).aspx
http://www.eggheadcafe.com/articles/20050625.asp
http://www.thescripts.com/forum/thread227492.html
 
09.17.2007 at 04:02AM PDT, ID: 19903958
Thanks for responce.

Ive played a little on how to call the finalize method. Also without making a call to the garbage collector as Alex's
Proposal suggests.

Public Class test
    Shared Sub Main()

        Dim ts As New test
        ts.Finalize()
    End Sub

    Protected Overrides Sub Finalize()

        Console.WriteLine("instance is Destroyed")

    End Sub
End Class

according to msdn
http://msdn2.microsoft.com/en-us/library/hks5e2k6(vs.80).aspx

<There is a slight performance penalty for executing Sub Finalize, so you should define a Sub Finalize method only <when you need to release objects explicitly.

What are actually the difference between the dispose and the finalize methods except the performance penalty ?


vbturbo

 
09.17.2007 at 04:57AM PDT, ID: 19904179

Rank: Sage

There is slight performance penalty because Finalize object is called directly and then GC.SuppressFinalize is called to prevent second finalizer call by garbage collector.
There are two reasons when .NET programmer should care about finalizer and disposing:
1. Class consumes unmanaged resources, like native handles, memory blocks etc.
2. Class contains IDisposable members.

All unmanaged resources should be released in the Dispose method. Every IDisposable class member should be disposed in the Dispose method. In the case client doesn't call Dispose, resources should be released in finalizer. The difference is that Dispose is called by client directly, and finalizer is called by garbage collector at undefined time. Unless you have unmanaged resources in your class, don't care about all this stuff.
MSDN article pointed in your post is good place to start learning this.
 
09.17.2007 at 05:30AM PDT, ID: 19904296
Hi Alex

So what you are sayings is, that when the Finalize object is called then garbage collector will SuppressFinalize  ? and taking the object off the finalization queue and prevent finalization code for this object from executing a second time . Right ?

Well this is more how to adopt a best practice and get the comprehension of this mecanishm when programming resource consuming projects!

vbturbo
 
09.17.2007 at 05:32AM PDT, ID: 19904313

Rank: Sage

The best practice is surely to use IDisposable and the Dispose() method. Is there a reason to use Finalize() instead?
Assisted Solution
 
09.17.2007 at 05:44AM PDT, ID: 19904376

Rank: Sage

Every type which consumes unmanaged resources must have Dispose method. However, not every client actually calls this method. In this case finalizer must make filan cleanup work - for lazy client which doesn't handle object by proper way. I think this is the only finalizer function.
Usually Dispose calls GC.SuppressFinalize(Me) to prevent GC to call finalizer, because there is no more work to do.
Accepted Solution
 
09.17.2007 at 08:45PM PDT, ID: 19910123
Finalize and Dispose Explained
http://msdn2.microsoft.com/en-us/library/ms998547.aspx#scalenetchapt05_topic11

Finalizer is to worked as last-resort, in case your Dispose didnt' get invoke due to the client code didnt invoke it (programmer forget it)

If your design can ensure your Dispose will be invoke, imo skip the Finalize . Finalize incurred performance hit, we do it just for "best practice".
I do it following the article, when unmanaged resources are involved only then I put in Finalize, esle Dispose will be sufficient.
Assisted Solution
 
09.18.2007 at 03:16AM PDT, ID: 19911477
thanks for inputs.
 
 
20080236-EE-VQP-29 / EE_QW_1_20070628