-
Jul 18th, 2021, 12:00 PM
#1
Thread Starter
Hyperactive Member
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
-
Jul 18th, 2021, 01:39 PM
#2
Re: verify COMObjectFromPtr object is valid
Code:
If myObject Is Nothing
should do the trick
-
Jul 18th, 2021, 01:48 PM
#3
Re: verify COMObjectFromPtr object is valid
Originally Posted by yokesee
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>
-
Jul 18th, 2021, 02:32 PM
#4
Thread Starter
Hyperactive Member
Re: verify COMObjectFromPtr object is valid
Originally Posted by Arnoutdv
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
Originally Posted by wqweto
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|