On Mar 24, 2006, at 8:33 PM, William Squires wrote:

Hi all!
  Okay, simple scenario here...

1) Add a new class "App" to a project, and a new window "wndMain".
2) Add

Public Sub Display()
  Self.Show()
End Sub

to wndMain. Make sure the Visible property is unchecked in the IDE.

3) Add a public property "DocumentWindow As wndMain" to the App class.
4) In the window's Close() event, just put "Quit"
5) In the App's Open() event, put:

Sub Open()
  Dim w As wndMain

  w = New wndMain
  DocumentWindow = w
  w.Display()
End Sub

6) Make a new class (call it whatever you want, I'm gonna call it QuackController for this example).
7) Make a constructor for QuackController

Public Sub QuackController(numQuacks As Integer)
  Dim m As MemoryBlock

  m = NewMemoryBlock(4)
  m.Long(0) = numQuacks
  fNumQuacks = m
End Sub

and a private/protected property "fNumQuacks As MemoryBlock"

8) Add a destructor "~QuackController()" as well, and - in it - nil out the memoryblock reference.

9) Provide a 'dummy' method - do whatever you want in it.

Public Sub Quack()
  Dim i As Integer
  Dim l As Integer

  l = fNumQuacks.Long(0)
  For i = 1 To l
    MsgBox "Quack!"
  Next
End Sub

put a pushbutton on wndMain to call App.TheQuackController.Quack(). (if you want...)

10) Add a public property "TheQuackController As QuackController" to the App class, and instantiate it in the App's Open() event before creating the main window.

11) nil out fQuackController in the App's Close() event.

Compile and run. (theoretically of course - this is just a thought experiment.)

Question now to the RB Gurus out there (yes, Joe and Charles, that's you...):

What's the sequence of events that occurs right when you click on the window's Close button?

Here's my guess:

1) RB runtime calls the window's CancelClose() event. If True is returned, nothing further happens. otherwise...
2) RB runtime now calls the window's Close() event.
3) Quit() is called (our code), telling RB runtime to clean up.
4) the App class's Close() event handler is called.
5) the TheQuackController instance is set to nil (our code).
6) the RB runtime now disposes wndMain through the DocumentWindow reference in the App class, as it's
now out of scope, thus causing wndMain to disappear.
7) wndMain's destructor is called (which does nothing, except perhaps clean up the controls.) The App
instance now dies, and is set to nil.
8) the RB runtime now looks for memory to release, and sees that TheQuackController has a reference count of zero, and frees the memory (probably with a dealloc() call, or similar) 9) This causes the ~QuackController() destructor to be called, which frees the memoryblock allocated earlier. 10) Having destroyed the TheQuackController instance, it continues iterating through it reference dictionary (?) and finds the MemoryBlock, and destroys it, too. 11) Finally, it sees the App instance memory, and frees it, too, then tells the OS, "hey, all done here!"

note that the order of #8-#111 is by no means guaranteed :)


You don't need to guess; just implement the code and step through it in the debugger. All you need to know in addition is that object destruction is deterministic in REALbasic; when an object's count goes to 0, it is destroyed right then. If in the course of the destruction of this object, the reference counts of other objects go to 0, then destruction of those objects occurs. I have a page on memory management in REALbasic that may explain this sort of thing.

--------------
Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to