does memoryblock.mid copy data or just reference it?

The new framework memoryblock methods of mid/left/right that return a new memory block. Does the memoryblock returned get a new copy of the data? or is the data referenced to the old memory block? Additionally, does it keep that memory around while it exists? Or do I need to keep the base memory block in scope until I’m done with the pieces?

As the MemoryBlock is mutable (read-only) it will use pointers to the original MemoryBlock.

MemoryBlocks will be garbage collected when they go out of scope, you can Nil the original at any time an the copies will still work.


[code] Dim a as xojo.Core.MemoryBlock
Dim b as xojo.Core.MemoryBlock

a = xojo.core.TextEncoding.UTF8.ConvertTextToData(“Hello”, True)
b = a.Left(5)

a = Nil

system.DebugLog xojo.Core.TextEncoding.UTF8.ConvertDataToText(b, True)

A quick test suggests that the data is copied.

  dim t as text = "this and that"
  dim mm as Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData( t )
  dim mmid as Xojo.Core.MemoryBlock = mm.Mid( 0, mm.Size )
  AddToResult Integer( mm.Data )
  AddToResult Integer( mmid.Data )
  if mm.Data = mmid.Data then
    AddToResult "same"
  end if

(AddToResult just appends text to a field as a new line, so modify as needed.)

The pointers do not match.

Currently this is true, but it’s something that the API (intentionally) makes possible in the future.