Why Glib::VariantBase::store method ruins the start of given buffer

113 Views Asked by At

The fun of working with Glib::VariantBase might be priceless. But brings up many obstacles with itself as well. I am having hard time to understand why the very basic Glib::VariantBase::store method is changing the start of my buffer.

Please assume i have a buffer allocated enough, better to write it down :

my_beautiful_buffer = static_cast<char*>(std::malloc(1024));

Then i would like to add a uint64_t variable to its beginning :

uint64_t my_humble_var = 1;
*reinterpret_cast<uint64_t*>(my_beautiful_buffer) = my_humble_var;

Lets read the buffer with printf

for (int i = 0; i < 8; i++) printf("0x%x ", *(unsigned char*)(my_beautiful_buffer+i));

+++++++++my_beautiful_buffer+++
0x1 0x0 0x0 0x0 0x0 0x0 0x0 0x0

Lets create a rather complex GlibVariable

using myStrangeVarType = Glib::Variant<std::map<Glib::ustring,std::map<Glib::ustring, std::tuple<Glib::VariantBase, std::uint64_t>>>>
myStrangeVarType data = createData(); // createData method statically defines variable and and copies

Now lets store this newly created variable

data.store((my_beautiful_buffer + sizeof(std::uint64_t)));

Can we read all the data in our beautiful buffer

for (int i = 0; i < data.get_size() + sizeof(std::uint64_t); i++) printf("0x%x ", *(unsigned char*)(m_buffer+i));

    +++++++++Data Written+++++++
    0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x61 0x70 0x70 0x49 0x64 0x31 0x2f 0x64 0x61 0x74 0x61 0x62
0x61 0x73 0x65 0x31 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x6b 0x65 0x79 0x31 0x0 0x0 0x0 0x0 0x73
0x74 0x72 0x69 0x6e 0x67 0x20 0x76 0x61 0x6c 0x75 0x65 0x0 0x0 0x73 0x0 0x62 0x67 0xbc 0x0 0x0
0x0 0x0 0x0 0xf 0x5 0x0 0x0 0x0 0x0 0x0 0x0 0x6b 0x65 0x79 0x32 0x0 0x0 0x0 0x0 0xd2 0x4 0x0
0x6e 0x0 0x0 0x0 0x0 0x62 0x67 0xbc 0x0 0x0 0x0 0x0 0x0 0x4 0x5 0x22 0x42 0x11 0x0

Okay At this point what happened to my first 8 bytes, why the very first byte 0x1 is vanished ?

1

There are 1 best solutions below

0
On BEST ANSWER

The problem has solved with some changes in the createData method. The method was producing a Glib::VariantBase with a{sa{s(vt)}} type, apparently packing and unpacking custom types is not really strong point of Glib::Variant.

I had to convert my object into just v type. And the problem has fixed. This is how i have changed the type:

GVariant* gVariant = g_variant_new("v", value.gobj());
Glib::VariantBase variantValue = Glib::VariantBase(gVariant);
return variantValue;

And earlier i was just returning the value without doing any of these conversions.


Another way to fix the problem was just calling value.get_size(); and return it afterwards.

value.get_size();
return value;

Apparently that call internally causes the variant to be serialized.