Results 1 to 8 of 8

Thread: What is the proper way to destroy an object?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Palm Desert, CA
    Posts
    91

    What is the proper way to destroy an object?

    If I understood correctly, when I set an object = to nothing then eventually the garbage collector comes around to destroy the object. However, the variable is still around to be reassigned. What is the right way to destroy an object. Even before the garbage collector comes around. I am creating many objects inside my classes and passing them around so I need to be as efficient as possible. So far all I can do is set them = to nothing when not in use.

  2. #2
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: What is the proper way to destroy an object?

    Everybody sais: garbage-collector does a good job.
    so set ur object to nothing and dont worry.
    u can put a new Value into a Var - that destroys the old value as well.

    Ähm - it only will be destroyed, if no other var refers to the object.

    take care: some classes implement IDisposable, and provide the .Dispose() - Sub.
    Those objects need to be destroyed by calling .Dispose().
    whats the benefit of beeing rated?


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Palm Desert, CA
    Posts
    91

    Re: What is the proper way to destroy an object?

    That is what I am doing now. You said it will only be destroyed if no other var refers to it. Is the following correct?
    Code:
    Class Engine
    
    End Class
    
    Dim TEST as new Engine
    
    Dim TEST2 as Engine = Nothing
    
    TEST2 = TEST
    
    TEST = Nothing
    Does this mean that nothing is referring to TEST at all and it will be destroyed? It seems pretty easy but I am making sure I understood you correctly. I have alot of this in my classes, so when I swap objects between between objects, I want to make sure I am not leaving an overhead in the class that is not using their objects.

  4. #4
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: What is the proper way to destroy an object?

    to understand objects exactly u must distinguish 3 entyties:
    1) the class: thats the code, its a "fabrication-plan".
    2) the object, item, (i dont know a good word in english): a range in memory, set up according to the fabrication-plan. This "fabricating" is done by the keyword "new".
    3) the var, that refers to the item. this is done by "=".

    the GC realizes, when there's no more var referring to an item anymore, and then it will clean up (free the range in memory).

    in ur sample, the var TEST2 still refers to the item, built by "new Engine", so the gc will not free that memory, allocated by the Engine-item.
    whats the benefit of beeing rated?


  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: What is the proper way to destroy an object?

    When you are done with an object, you can set it to nothing. Eventually the GC will get around to cleaning it up when it's efficient to do so (or memory is running low).

    If I have two variables, both pointing to the same data (as is the case of TEST and TEST2 above)... setting one to Nothing does not destroy the data... actually it never does. What it really does is mark that memory as "free" and sets the pointer (which is what the variable really is) to NIL (or empty). Kind of like re-setting the trip odometer in the car. When the GC comes around, it finds the data, pops it head over the cube wall (also known as "ground hogging") and asks "Anyone using this?" ... if it is not in use, then the memory is cleared.

    If you are creating and destorying a lot of objects and want to be as efficient with the memory as you can.... then consider calling .Dispose on the object BEFORE setting it to nothing. This should allow the GC to enact on the object right away, and give the object a chance to cleanup any resources it was using.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: What is the proper way to destroy an object?

    If you want to be REALLY efficient, then don't create and destroy a bunch of objects in the first place. For example, I was working on a biological model, where the population would be made up of lots of animals of various types. Each animal was a class. When an animal died, it made sense to destroy it, and when an animal was born, it made sense to create it. However, this meant there was lots of objects being created and destroyed, which was less than ideal at the time (unmanaged C++ with no GC).

    My solution was to make an array of objects of each animal, and add a single boolean to the class indicating alive state. When an animal died, I could toggle the boolean to False. When I needed to create a new animal, I toggled the boolean to True, and had an Initialize sub that set any necessary variables. Therefore, the objects were all created one time at startup and never again.

    If you are concerned about creating and destroying lots of variables due to memory issues, something like that would be worth considering, though there are some refinements that could be added in .NET, such as using a List(of Engines) rather than an array, and using either a Stack (of Engines) or a Queue (of Engines) to keep a record of which ones are not in use.
    My usual boring signature: Nothing

  7. #7
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: What is the proper way to destroy an object?

    If the object you are using implements IDisposable then you can also use the Using statement to ensure proper cleanup. Using the above example, you can do this, if it were to implement IDisposible.

    Code:
    Using myEngine As New Engine
        ' use myEngine here
    End Using
    This will ensure proper cleanup even if an error occurs.

    Here is more information.

    http://msdn.microsoft.com/en-us/libr...hh(VS.80).aspx
    Ben


    Using Visual Basic 2005/2008

  8. #8
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: What is the proper way to destroy an object?

    i say again: dont worry.
    usual objects just set to nothing.
    Explicit Disposing is a special case, and u must take care, if ur object Implement IDisposable, but if not - dont worry.
    Strong optimizations usually aren't needed. Only implement complicated stuff like that, if u realize there's a heavy performance-leak caused of creating and destroying objects.

    see the MOuseMove-Event: At any mousemove (that may be up to 100 within one second) the control-class creates a MouseEventArgs - Object and destroys it (exact: leaves it to the GC to cleanup) after raising the event - no problem with that.
    whats the benefit of beeing rated?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width