Quote:
Originally Posted by whatsup
Ok, that way I agree. Still, if short of memory you can deallocate memory earlier using Set to Nothing. But otherwise it's done automatically when leaving the sub. That's shown nicely in Run11, when you skip the part Set to nothing, the Terminate-Event of the class is still triggered (but I see you mentioned this already in your post)
|
OK, cool

And yes, I agree, its a good idea to set objects to nothing earlier if you don't need them anymore... but if you are already at the end of the sub then... yep, we agree on that.
Quote:
Originally Posted by whatsup
And I remember very well that you argue against - therefore we are here  But I enjoy. That way I learn quite a lot about memory, which really is due in regard that obviously MS has improved on the subject.
|
I am enjoying the learning also!
Quote:
Originally Posted by whatsup
Yeah, the VarPtr of an object - honestly I don't know if it makes sense. I tried to figure out whether the Mem_ReadHex of VarPtr points to the ObjPtr by converting the HexToDec, but was disapointed by the result. Even worse HexToDec comes up with negative figures, so what shall we make of this? Maybe Val("&H"&...) isn't the right tool?
|
Ah, this one I can explain...
The memory image is in a format called little endian. Intel-based systems store bytes little end first so the least significant byte is on the left. You have to flip the bytes to see the true hex value...
If you look at the last section of getReference2() in my previous post you will see how that I move the contents of the local pointer (the address of Sheet1) into another LongPtr type called objwksPtr. I can then print out the hex value of that pointer and VBA will interpret it correctly.
Code:
01 Sheet1 : Address: 0x1D6DA618 : Contents: 0xC0EA4C18 using objPtr
02 objwks : Address: 0x002AEF90 : Contents: 0x00000000 using varPtr, set to Nothing
03 objwks : Address: 0x1D6DA618 : Contents: 0xC0EA4C18 using objPtr
04 objwks : Address: 0x002AEF90 : Contents: 0x18A66D1D using varPtr
05 objwks points to: 0x1D6DA618
Line 04 is from this...
' Using varPtr, can diferentiate.
' The local Worksheet Object is treated as a reference, not an Object: the reference is not resolved
lptr = VarPtr(objwks): objName = "objwks "
Debug.Print PointerToSomething(objName, lptr, coBytes) & vbTab & "using varPtr"
It produces this image of the contents of the objwks reference
0x18A66D1D
Line 05 is from this
' The contents of the local variable (objwks) is the address of the global object but byte-reversed (little endian)
' Load the reference into a LongPtr type and read back the byte-corrected (big endian) value of the reference
Mem_Copy objwksPtr, ByVal VarPtr(objwks), PTR_LENGTH
Debug.Print objName & vbTab & "points to: " & "0x" & HexPtr(objwksPtr)
It produces this image of the contents of the objwks reference
0x1D6DA618
And this is the correct address of the Sheet1 Object
But in fact they are the same number but in different formats...
1D6DA618 18A66D1D
I just did it like that for illustration, the other way is to shuffle the bytes in the Mem_ReadHex routine. I added a sub called Mem_ReadHex_Endian_Aware to your Mod00_PublicFunctionsAPI module and incorperated it into your pointerToSomething routine in the attached version of your spreadsheet.
Ive also attached a couple of pics to try to make it clear...