Search This Blog

Monday, August 25, 2014

"Delete doc" vs "set doc = nothing"

Hi

Just noticed a funny thing regarding using "Delete doc" against "set doc = nothing".

I always though if you delete a local reference to some object declared inside of sub/function it will not affect on another reference to the same object but in parent sub/function.
It's like a common thing about variables scope.

However it looks like it works differently with objects received from NotesUIWorkspace (and probably NotesSession). May be because NotesUIWorkspace and NotesSession are global objects, not sure.

Look on example below:

Sub Click(Source As Button)
Dim w As New notesuiworkspace
Dim doc As notesdocument

Set doc = w.CurrentDocument.Document

Call mySub()

Msgbox doc.Created <--Error here
End Sub
Sub mySub()
Dim w As New notesuiworkspace Dim note As notesdocument Set note = w.CurrentDocument.Document Delete note
End Sub


Sub Click(Source As Button)
Dim w As New notesuiworkspace
Dim doc As notesdocument

Set doc = w.CurrentDocument.Document

Call mySub()

Msgbox doc.Created <--No error
End Sub
Sub mySub()
Dim w As New notesuiworkspace Dim note As notesdocument Set note = w.CurrentDocument.Document set note = nothing
End Sub


5 comments:

  1. That makes perfect sense though - Set note = nothing just clears your pointer to the note the you created outside the Sub, Delete note actually deletes the note. Nothing to see here, move along :o)

    ReplyDelete
    Replies
    1. Hi Ursus
      Thank's for reply, I have removed my first reply on your comment, I was wrong, sorry.
      But let me explain.
      I know what is the difference between 'Delete' and 'Set to Nothing'.
      The reason (why I didn't expect it) was that I explicitly created a NEW instance of the class NotesUIWorkspace and I expected that all variables, children objects and references to such objects should be allocated in heap separately. But it seems that lotus run-time does not create a new instance of the same backend object (I mean the same notesdocument or notesdatabase) - it just creates a new reference to it but keep only one object in memory. that's why statement 'delete' affects on any reference to the same object.
      On the other hand, there are other Notes classes which behave as I expected, like NotesStream, NotesDbDirectory, NotesName,....
      So correct answer is that Notes run-time always keep only one instance of the same backend object in memory that's why 'Delete' statement affects on ALL references to this object no matter the scope.

      Delete
    2. This comment has been removed by a blog administrator.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by a blog administrator.

      Delete