Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Allocating/freeing objects, revisited...

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
Alright, i am still not sure when I should implement garbage-collection in VB. They've made it insanely difficult and blurry, and after two weeks of attempting to figure out when to implement dispose, I am worn out. I have my own classes, but they contain standard classes, such as Integer, String, etc. One of them contains other classes of my own design however, and I don't have a clue how to tell if I need to free them somehow.
[code]
Public Class systemClass
'Global objects
Private uppermemBool As Boolean
Private expandedBool As Boolean
Private extendedBool As Boolean
Private memoryShort As Short
Private cpuShort As Short
Private cyclesInt As Integer
...
End Class

Public Class gameClass
'Global objects
Private initializedBool As Boolean
Private systemObject As systemClass
Private videoObject As videoClass
Private soundObject As soundClass
...
End Class
[/code]
In the example above, the second class uses an instance of the first class, but the first class only contains built-in classes. My main form creates an instance of the second class, then class an initialization function that allows that class to create instances of the three custom classes inside it.

At this point if the program exits, I simply set the second class to "Nothing", but does that free everything? How about a simple call to "free" like every other language out there? I also can't find info on implementing a destructor in VB, which is confusing as heck because you have to have a constructor!
[code]
Public Class mainForm
Private gameObject As gameClass

Private Sub mainForm_Load(...) Handles ...
Try
Me.gameObject = New gameClass
Catch
Application.Exit()
End Try
End Sub

Public Sub exitButton_Click(...) Handles ...
Me.gameObject = Nothing 'Does this free it like free() in C?
End Sub
End Class
[/code]
So is this freeing everything, or should I write a method in the gameClass object that I can call to set the three custom classes in it to "Nothing" before closing? This is just a huge gray area right now and I just need to know how and when to use dispose, or how to write a destructor.

Oh, and what happens if the program closes in another way, such as the user clicking the X in the upper-right of the window? My code wouldn't even set the gameObject to "Nothing"!

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • zibadianzibadian Member Posts: 6,349
    : Alright, i am still not sure when I should implement
    : garbage-collection in VB. They've made it insanely difficult and
    : blurry, and after two weeks of attempting to figure out when to
    : implement dispose, I am worn out. I have my own classes, but they
    : contain standard classes, such as Integer, String, etc. One of them
    : contains other classes of my own design however, and I don't have a
    : clue how to tell if I need to free them somehow.
    : [code]:
    : Public Class systemClass
    : 'Global objects
    : Private uppermemBool As Boolean
    : Private expandedBool As Boolean
    : Private extendedBool As Boolean
    : Private memoryShort As Short
    : Private cpuShort As Short
    : Private cyclesInt As Integer
    : ...
    : End Class
    :
    : Public Class gameClass
    : 'Global objects
    : Private initializedBool As Boolean
    : Private systemObject As systemClass
    : Private videoObject As videoClass
    : Private soundObject As soundClass
    : ...
    : End Class
    : [/code]:
    : In the example above, the second class uses an instance of the first
    : class, but the first class only contains built-in classes. My main
    : form creates an instance of the second class, then class an
    : initialization function that allows that class to create instances
    : of the three custom classes inside it.
    :
    : At this point if the program exits, I simply set the second class to
    : "Nothing", but does that free everything? How about a simple call
    : to "free" like every other language out there? I also can't find
    : info on implementing a destructor in VB, which is confusing as heck
    : because you have to have a constructor!
    : [code]:
    : Public Class mainForm
    : Private gameObject As gameClass
    :
    : Private Sub mainForm_Load(...) Handles ...
    : Try
    : Me.gameObject = New gameClass
    : Catch
    : Application.Exit()
    : End Try
    : End Sub
    :
    : Public Sub exitButton_Click(...) Handles ...
    : Me.gameObject = Nothing 'Does this free it like free() in C?
    : End Sub
    : End Class
    : [/code]:
    : So is this freeing everything, or should I write a method in the
    : gameClass object that I can call to set the three custom classes in
    : it to "Nothing" before closing? This is just a huge gray area right
    : now and I just need to know how and when to use dispose, or how to
    : write a destructor.
    :
    : Oh, and what happens if the program closes in another way, such as
    : the user clicking the X in the upper-right of the window? My code
    : wouldn't even set the gameObject to "Nothing"!
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    If VB does indeed have garbage-collection, then you don't need to implement anything. If an object is not referenced by any reachable code, then it and all its fields (and their fields, etc.) will be freed and their memory reclaimed.
    Often only objects with special resources (open files, network links, etc.) need to have specialized code to correctly release those resources. This is often left to the programmer in the way of close()/dispose() methods.
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    : If VB does indeed have garbage-collection, then you don't need to
    : implement anything. If an object is not referenced by any reachable
    : code, then it and all its fields (and their fields, etc.) will be
    : freed and their memory reclaimed.
    : Often only objects with special resources (open files, network
    : links, etc.) need to have specialized code to correctly release
    : those resources. This is often left to the programmer in the way of
    : close()/dispose() methods.

    Yeah i read about that, but nothing has explicitly said that a custom class containing other custom classes does not need to free those other custom classes before it closes. Nothing has said I have to either, but if I understand you correctly, in VB.NET 2005, unless my class has an open file handle or something, I am good to go and can literally close the app without setting anything to "Nothing", and those custom classes will be freed so long as they're not handling files or other special cases?
    [code]
    Public Class dataClass
    Private myString As String
    ...
    End Class

    Public Class customClass
    Private myInt As Integer
    Private myData As dataClass

    Public Sub New()
    Me.myInt = -32768
    Me.myData = New dataClass
    End Sub
    End Class

    Public Class mainForm
    Private customObject As customClass

    Me.customObject = New customClass
    Application.Exit()
    End Class
    [/code]
    In the short, lame example above, would everything be freed proeprly, assuming that mainForm was the startup form?

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • zibadianzibadian Member Posts: 6,349
    : : If VB does indeed have garbage-collection, then you don't need to
    : : implement anything. If an object is not referenced by any reachable
    : : code, then it and all its fields (and their fields, etc.) will be
    : : freed and their memory reclaimed.
    : : Often only objects with special resources (open files, network
    : : links, etc.) need to have specialized code to correctly release
    : : those resources. This is often left to the programmer in the way of
    : : close()/dispose() methods.
    :
    : Yeah i read about that, but nothing has explicitly said that a
    : custom class containing other custom classes does not need to free
    : those other custom classes before it closes. Nothing has said I
    : have to either, but if I understand you correctly, in VB.NET 2005,
    : unless my class has an open file handle or something, I am good to
    : go and can literally close the app without setting anything to
    : "Nothing", and those custom classes will be freed so long as they're
    : not handling files or other special cases?
    : [code]:
    : Public Class dataClass
    : Private myString As String
    : ...
    : End Class
    :
    : Public Class customClass
    : Private myInt As Integer
    : Private myData As dataClass
    :
    : Public Sub New()
    : Me.myInt = -32768
    : Me.myData = New dataClass
    : End Sub
    : End Class
    :
    : Public Class mainForm
    : Private customObject As customClass
    :
    : Me.customObject = New customClass
    : Application.Exit()
    : End Class
    : [/code]:
    : In the short, lame example above, would everything be freed
    : proeprly, assuming that mainForm was the startup form?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    Yes. Because when the mainForm instance is released, the customObject is set to Nothing. This removes the one (assuming no other references exist) reference to customClass. This will then be G.C.ed, thereby releasing the myData. This will make myData eligable for G.C.
    Note: often programmers starting with OOP make a difference between custom designed classes and existing classes. There is no such difference. From a compiled program, there is no distiction between them. In many OOP-languages it is possible to recreate the standard classes, although the recreations are often slower than the standard ones.
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    : Note: often programmers starting with OOP make a difference between
    : custom designed classes and existing classes. There is no such
    : difference. From a compiled program, there is no distiction between
    : them. In many OOP-languages it is possible to recreate the standard
    : classes, although the recreations are often slower than the standard
    : ones.

    It's not that I am starting with OOP, the problem is that I have been doing C++ (OOP) for years, and you know you have to clean up after yourself! Every other language on God's green earth requires you to call free() or delete or something, whereas VB.NET stands out and apparently does not require this. That is what had me paranoid. I am a VERY tidy programmer in C++ and I always free used memory upon class destruction, and not having to do it here was just not sitting well with me.
    [code]
    char *pBuffer;

    pBuffer = new char[4096];
    [/code]
    That eats 4kb of RAM, and if you don't add "delete [] pBuffer;" to the class destructor, that 4kb will remain tied up after the class is no longer in use, which is a nice memory-leak if you use that class frequently. That's why I was so worried about not freeing everything in VB, but you've cleared it up for me. Thanks for your help, again!

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • zibadianzibadian Member Posts: 6,349
    : : Note: often programmers starting with OOP make a difference between
    : : custom designed classes and existing classes. There is no such
    : : difference. From a compiled program, there is no distiction between
    : : them. In many OOP-languages it is possible to recreate the standard
    : : classes, although the recreations are often slower than the standard
    : : ones.
    :
    : It's not that I am starting with OOP, the problem is that I have
    : been doing C++ (OOP) for years, and you know you have to clean up
    : after yourself! Every other language on God's green earth requires
    : you to call free() or delete or something, whereas VB.NET stands out
    : and apparently does not require this. That is what had me paranoid.
    : I am a VERY tidy programmer in C++ and I always free used memory
    : upon class destruction, and not having to do it here was just not
    : sitting well with me.
    : [code]:
    : char *pBuffer;
    :
    : pBuffer = new char[4096];
    : [/code]:
    : That eats 4kb of RAM, and if you don't add "delete [] pBuffer;" to
    : the class destructor, that 4kb will remain tied up after the class
    : is no longer in use, which is a nice memory-leak if you use that
    : class frequently. That's why I was so worried about not freeing
    : everything in VB, but you've cleared it up for me. Thanks for your
    : help, again!
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    I know the feeling. I had the same thing when I changed from Delphi (non-GC OOP) to Java (GC OOP).
  • JonathanJonathan Member Posts: 2,914
    : Every other language on God's green earth requires
    : you to call free() or delete or something, whereas VB.NET stands out
    : and apparently does not require this.
    Every other language? Some other well known languages that handle memory management for you, like VB.NET, include C# (which if you need to do .Net programming and come from a C/C++ background, may have syntax that makes you feel more at home), Java, Perl, Python, Ruby, PHP, ML... In the work I do, the times I have to do my own memory management are limited to the few times a month I need to do some stuff in C. But anyways...

    The .Net collector is reachability based. Imagine all the objects with arrows pointing between them showing what is referenced by what. Then there is a "root set" which is the things that we know the program can still reach. The collector works out from this which things are reachable. If you have got some bunch of objects that reference each other, but aren't reachable by program code any more, it knows it can free the memory associated with the bunch of them. (So with the .Net collector, circular references aren't a problem.) Hopefully knowing roughly how it works makes it feel a tad less magical. :-)

    Also, I believe the .NET one runs asynchronously (in a different thread), so it has minimal impact on your own running code and in a small way helps you make use of multi-core a tad better.

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
Sign In or Register to comment.