New MemoryBlock seems to return the same block

Hello,

I try to have a memoryBlock containing pointers to other MemoryBlock. At the end of this sample code v1 equal v0 and each pointer of lut points to the same MemoryBlock. I get the same result with an array of Ptr. I tried to subclass MemoryBlock unsuccessfully. I’ve probably forgotten something and/or made a mistake somewhere, but where ?

Thanks for your lights

Const pHeight = 2160
Const sizeofPtr = 8
Const sizeofDouble = 8
Const sizeofSingle = 4

Var v0, v1 As Double
Var lut As New MemoryBlock(pHeight * sizeofPtr)

// If Not lut IsA MemoryBlock Then Return 

For i As Integer = 0 To lut.Size - 1 Step sizeofPtr
  lut.Ptr(i) = New MemoryBlock(pHeight * sizeofDouble)
Next

v0 = 3.14159265358979323846
lut.Ptr(0).DoubleValue(0) = v0
v1 = lut.Ptr(24).DoubleValue(0)

Break

You must keep the MemoryBlock around as ptr won’t retain a reference to it. Try adding the new MemoryBlock to an array that survives the loop.

1 Like

Thanks @Kem_Tekinay.

I need to change my plan as I wanted to pass lut to a plugin I’m working on and use it like a 2D memory space.