MemoryBlock for Object

What is the best/proper way to obtain a memory block with the contents of an object; either by creating a memory block overlapping the object’s content, or copying the content into a new Memory block.

My goal is to copy the bytes(content) of an object into a memory block for examination of how Xojo does certain alignments among other reasons.

In general, Xojo doesn’t allow this - it’s not easy to get a pointer to an object, and if you do, there are no guarantees about the in-memory format.

What is the end goal? If you are trying to do something where memory layout is important, you may want to use a Structure instead.

If you are trying to serialize an object, you probably need to write custom code to do that on a per-object basis.

I would not recommend it, but you could get the ptr to an object via plugin.

1 Like

It would not be valid by any means.

Object can and will have other pointers inside of it, pointing to yet another data. As in…in 99% of cases Object does not just have simple struct data inside of it.

1 Like

An Object is a virtual entity that physically can be constituted by sparse contents anywhere in the memory, so it’s not a continuous stream of bytes you can copy/past anywhere. People desiring such capability need to implement what is called “serialization”, that means grab an frozen state of an object and convert its properties to a series of bytes that can be deserialized later into a copy of that prior object.

Yes, after a little more thought, Structures are a good solution to the real problem I want to solve. Gives me control over how I do it, not how Xojo does.

Note that Structures behave differently from Objects. When you assign a Structure, you create a copy. This is true whether you pass it as a parameter to a function (unless you use ByRef) or assign it to a different local variable.

Yes, I’m aware of the copy aspect; which is not a problem for me. What concerns me more is efficiency on the M2 processor for things like storing, reading doubles that may or may not be aligned to say 2 or 4 byte offsets. I’m not sure if it matters or I should care.

The compiler aligns those always when laying out the data structures for objects.

And since all memory allocations are 16 byte aligned, there is usually never a problem with bad alignment.