-
Jul 22nd, 2021, 08:35 AM
#1
Thread Starter
Fanatic Member
Check if Pointer is valid ?
Hi,
Is there a way of figuring out if an object pointer is valid ? (ie:- points to a valid instanciated object)
Thanks.
-
Jul 22nd, 2021, 09:25 AM
#2
Re: Check if Pointer is valid ?
Once you start fiddling with interface pointers you have broken the contract and all bets are off. You must take on full responsibility at that point.
IUnknown.Release() returns the remaining object reference count, but there is no requirement that it be accurate, only 0 or not 0. Once Release() is down to 0 references, the method releases the object.
Without more context we can't answer this vague question.
-
Jul 22nd, 2021, 10:07 AM
#3
Thread Starter
Fanatic Member
Re: Check if Pointer is valid ?
Ok- let me explain what I am trying to do.
Basically, I have a class and I have some code that is meant to be ran upon initializing the class (ie:- inside the Class_Initialize event).
The issue is, I want the initialize code to be ran ONLY for the first class instance... Subsequent class instances should skip the initialize code.
This is what I am doing ... I just need the necessary code for the IsValidPointer function.
1- Code of the class client:
Code:
Option Explicit
Dim Obj1 As Class1, Obj2 As Class1
Sub Test()
Set Obj1 = New Class1
SetProp hwnd, "POINTER", ObjPtr(Obj1)
Set Obj2 = New Class1
End Sub
2- Class code :
Code:
Option Explicit
Private Sub Class_Initialize()
If IsValidPointer(GetProp(hwnd, "POINTER")) = False Then
'\\ run some code only for first class instance
End If
End Sub
Private Function IsValidPointer(ByVal Ptr As Long) As Boolean
End Function
-
Jul 22nd, 2021, 10:48 AM
#4
Re: Check if Pointer is valid ?
GetProp should return 0 on first instance call so IsValidPointer probably can be reduced to IsValidPointer = (Ptr <> 0)
cheers,
</wqw>
-
Jul 22nd, 2021, 10:59 AM
#5
Re: Check if Pointer is valid ?
I have no idea what you are really trying to accomplish, just the wacky way you want to go about it.
If you GetProp() a window handle and it has never been defined a NULL (0) is returned. So all you really need to do is call SetProp() the first time passing something non-NULL (for example 1). And don't forget the requirement to RemoveProp() as well.
But the entire enterprise looks dicey to me. You seem to be trying to reinvent the concept of a class factory by severely breaking encapsulation using a trick meant for entirely different purposes.
This is clearly a job for a DLL. There you would have a creatable public class that is the class factory. When you ask it to create an instance of another non-creatable public class for you it can do any yet-uncompleted initialization there, probably ending up assigning values to data in a static module visible to the non-creatable class.
If you are playing reindeer games trying to write monolithic programs, then do the same thing. You must hold your mouth right, cross your fingers, and hope for the best by never referencing that static module data elsewhere and never creating instances of the second class explicitly in the client code. This is brittle though... because "playahs gonna play."
But screwing around with window property lists is a newb move. Exactly the sort of thing that gets people fired.
Every time I see a stunt like this I begin to understand why Microsoft killed VB and pushed .Net instead.
-
Jul 22nd, 2021, 11:05 AM
#6
Thread Starter
Fanatic Member
Re: Check if Pointer is valid ?
Originally Posted by wqweto
GetProp should return 0 on first instance call so IsValidPointer probably can be reduced to IsValidPointer = (Ptr <> 0)
cheers,
</wqw>
Thanks... I thought about that but I wanted to make this foolprof so that if the vba project was reset by accident and there was a state loss and therefore the class Terminate-event wouldn't cleanup & reset the hwnd Prop "ie:= SetProp hwnd,"POINTER",0" or RemoveProp.
I could reset the hwnd Prop in the class client code at the very start of the Test routine but, that was just an simplified example. The code I have is more involved so I would prefer to cleanup the hwnd Prop in the class Terminate event.
-
Jul 22nd, 2021, 11:13 AM
#7
Thread Starter
Fanatic Member
Re: Check if Pointer is valid ?
Originally Posted by dilettante
If you GetProp() a window handle and it has never been defined a NULL (0) is returned. So all you really need to do is call SetProp() the first time passing something non-NULL (for example 1). And don't forget the requirement to RemoveProp() as well.
I will probably do that (Also, same suggestion offered by wqweto)
Thanks.
-
Jul 23rd, 2021, 09:16 AM
#8
Re: Check if Pointer is valid ?
There is also
Private Declare Function IsBadReadPtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long
API function which is totally *not* what you want but if this returns non-zero for sure the lp pointer is not valid.
It might return false positives though -- an object is deallocated but the heap (virtual memory) it was created in is still allocated in the current processs (and good to read).
Besides, there is no reliable way to figure out instance size for the ucb parameter. Probably something like 4 (or 8) is theoretical lower bound though.
cheers,
</wqw>
-
Jul 24th, 2021, 09:24 AM
#9
Re: Check if Pointer is valid ?
You're making this way harder than it needs to be. You can easily implement the behavior you want by utilizing a global variable accessible by all instances of the class. It could be a simple Boolean. Every time a new object is created, have it check this Boolean, if that Boolean value is False, run the initialization code and set it to True. Every instance after that will see the Boolean being True and not run the initialization code.
That fact that you didn't consider this solution tells me there is something else going on here. My answer above is based completely and exactly on what you said here:-
Originally Posted by JAAFAR
Ok- let me explain what I am trying to do.
Basically, I have a class and I have some code that is meant to be ran upon initializing the class (ie:- inside the Class_Initialize event).
The issue is, I want the initialize code to be ran ONLY for the first class instance... Subsequent class instances should skip the initialize code.
I sort of skimmed through the thread to get to the meat of the issue so if there is something else I missed, my apologies if this is not what you want.
Last edited by Niya; Jul 24th, 2021 at 09:30 AM.
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
|