Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Allocating/freeing objects, revisited...
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.
Public Class systemClass
Private uppermemBool As Boolean
Private expandedBool As Boolean
Private extendedBool As Boolean
Private memoryShort As Short
Private cpuShort As Short
Private cyclesInt As Integer
Public Class gameClass
Private initializedBool As Boolean
Private systemObject As systemClass
Private videoObject As videoClass
Private soundObject As soundClass
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!
Public Class mainForm
Private gameObject As gameClass
Private Sub mainForm_Load(...) Handles ...
Me.gameObject = New gameClass
Public Sub exitButton_Click(...) Handles ...
Me.gameObject = Nothing 'Does this free it like free() in C?
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"!