Results 1 to 4 of 4

Thread: verify COMObjectFromPtr object is valid

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    506

    verify COMObjectFromPtr object is valid

    Hi all,
    How to verify that an object is valid and has not been deleted.
    Using this code to get and ObjPtr object.

    https://www.vbforums.com/showthread....=1#post5161749
    Code:
    Private Declare Function vbaObjSetAddref Lib "MSVBVM60.DLL" _
                             Alias "__vbaObjSetAddref" ( _
                             ByRef dstObject As Any, _
                             ByRef srcObjPtr As Any) As Long
                             
    Private Sub Form_Load()
        Dim frm As Form1
        
        Set frm = COMObjectFromPtr(ObjPtr(Me))
        
        frm.Circle (100, 100), 100
        
    End Sub
    
    ' //
    ' // Get COM object from pointer
    ' //
    Public Function COMObjectFromPtr( _
                    ByVal ptr As Long) As IUnknown
        vbaObjSetAddref COMObjectFromPtr, ByVal ptr
    End Function
    If by some mistake I delete the object.
    How to verify that the variable is not a valid object, the program does not close when using it.

    Thanks

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: verify COMObjectFromPtr object is valid

    Code:
    If myObject Is Nothing
    should do the trick

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,121

    Re: verify COMObjectFromPtr object is valid

    Quote Originally Posted by yokesee View Post
    If by some mistake I delete the object.
    How to verify that the variable is not a valid object, the program does not close when using it.
    You can't.

    When storing so called "weak references" (the naked pointer from ObjPtr) there is a potential risk of re-hydrating a "dangling" pointer (one that points to an already deallocated instance).

    Weakrefs are useful only for preventing circular references and nothing else so don't use these instead of "regular" VB6 compatible object references.

    I usually only use weakrefs in private "child" instances which get explicit cleanup call from the "parent" instance (the one that weakref is pointing at) in its Class_Terminate event just to clean up the weakref pointers in children so these do no get re-hydrated anymore from this point on.

    cheers,
    </wqw>

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    506

    Re: verify COMObjectFromPtr object is valid

    Quote Originally Posted by Arnoutdv View Post
    Code:
    If myObject Is Nothing
    should do the trick
    no that way it doesn't work, with objects that have been deleted it will crash all
    thank
    Quote Originally Posted by wqweto View Post
    You can't.

    When storing so called "weak references" (the naked pointer from ObjPtr) there is a potential risk of re-hydrating a "dangling" pointer (one that points to an already deallocated instance).

    Weakrefs are useful only for preventing circular references and nothing else so don't use these instead of "regular" VB6 compatible object references.

    I usually only use weakrefs in private "child" instances which get explicit cleanup call from the "parent" instance (the one that weakref is pointing at) in its Class_Terminate event just to clean up the weakref pointers in children so these do no get re-hydrated anymore from this point on.

    cheers,
    </wqw>
    Thank you very much for the information, I will keep it in mind

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width