none
Can I store a reference to a tom.ITextDocument object in a variable for a long time? RRS feed

  • Question

  • I use the message EM_GETOLEINTERFACE to create a tom.ITextDocument object and keep the reference in a global variable.

    I've been experimenting and it appears that the variable loses its connection to the RichTextBox.

    Can you explain that?

    Do I have o create the object just before I use it and release it right after the use? This would happen basically every time the user clicked a menu item.

    Can I store a reference to a tom.ITextDocument object in a variable for a long time?

    Thursday, May 11, 2017 10:04 PM

All replies

  • I will begin with, I have no idea what ITextDocument does other than a quick Google search yet it's an object and being an object see no reason why your connection would be lost other than an interaction with the RichTextBox causing it to lose said connection. An object lives until it's disposed of via code written or the control it interacts with.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, May 11, 2017 10:34 PM
    Moderator
  • Can I store a reference to a tom.ITextDocument object in a variable for a long time?

    What is the symptom that you are seeing?  That will indicate whether the 'connection' is disappearing because of the variable that you are using or because the object disappears.

    You should show the code you using to get the interface to the object.  In particular you need to pay attention to the declaration of the variable you are using, as the runtime is not managing the object it refers to.
    https://msdn.microsoft.com/en-us/library/kzd63x5w.aspx

    Thursday, May 11, 2017 10:46 PM

  • You should show the code you using to get the interface to the object.  In particular you need to pay attention to the declaration of the variable you are using, as the runtime is not managing the object it refers to.

      At first I did things to a Range that did not show up in the RichTextBox. Later I did things like the following code:

    The qq range print correlated with the RichTextBox while the g range simply printed VbCr.

    GetITextDocument uses the message EM_GETOLEINTERFACE to create a tom.ITextDocument object.

    I checked your reference and wouldn't be surprised if the answer is there but it is not evident to me. I believe that since the gITextDocument is declared in a Module it is Static. Correct?

    I don't believe the variable is the problem since I have many variables in the Module that are not troublesome. It's COM that I am not familiar with.

    Private Sub Timer_JUNK_Tick(sender As Object, e As EventArgs) Handles Timer_JUNK.Tick
            Dim qq As tom.ITextDocument = GetITextDocument()

            If gITextDocument.Range(0, 19).Text <> qq.Range(0, 19).Text Then
                Debug.WriteLine(" \ " & gITextDocument.Range(0, 19).Text & " $ " & qq.Range(0, 19).Text & " % " & RichTextBox_Txt.Handle.ToInt64.ToString)
            End If
        End Sub

    Thursday, May 11, 2017 11:53 PM
  • I don't believe the variable is the problem since I have many variables in the Module that are not troublesome. It's COM that I am not familiar with.

    Show the code for GetITextDocument. That will indicate how the return value is being validated.

    I presume it is qq that is becoming corrupted - I can't see what gITextDocument or q are. It is not the scope of GetITextDocument that you need to be concerned about (because the compiler will warn you if it is not accessible from that code), but the lifetime (in this case) of qq.  You need a much simpler test for qq than the comparison that you are using, so that you can spread that test throughout your code to determine the point at which the reference becomes invalid.  That will allow you to determine whether the variable is losing its reference (being set to Nothing, or to an invalid value) or the object is disappearing.

    Friday, May 12, 2017 12:51 AM