# Help with MemoryBlock and ValueTree

**URL:** <https://forum.juce.com/t/help-with-memoryblock-and-valuetree/17884>\
**Category:** General JUCE discussion\
**Created:** [May 28, 2016, 6:28pm UTC](https://forum.juce.com/t/help-with-memoryblock-and-valuetree/17884 "2016-05-28T18:28:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![juan1979](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@juan1979](https://forum.juce.com/u/juan1979)\
**Post date:** [May 28, 2016, 6:28pm UTC](https://forum.juce.com/t/help-with-memoryblock-and-valuetree/17884/1 "2016-05-28T18:28:22Z")

</div>

Hi, I have a big amount of data in a project that needs be written. To be exact, I need to write 16 x 16 x 16 x 64 x 90 = 23 Megabytes.

My original idea was creating a ValueTree, since it’s an structure of classes and subclasses. However it last around 300 seconds into creating the ValueTree and convert it to binary (more when I read the binary). Too much. Then I tried to write a MemoryBlock with the lastes 64 x 90 bytes.

This is the function I’m using to write it:

```
` MemoryBlock memBlock;
   for (int8 i=0; i<64; ++i)
   {
      const Car* const currentCar= m_cars.getUnchecked(i);
         for (int8 j=0; j<90; ++j)
         {
            const int8 currentProp = currentCar->getProperties(j);
            memBlock.append(&currentProp, 1); // Maybe this is CRAZY!!
         }
      }
   }

   ValueTree tree("AllCars"); 
   tree.setProperty("S", memBlock, 0); // I will write few more properties later, but only a bunch.

```

And this one is how I read it once I convert the root MemoryBlock to a ValueTree:

```
   const var* const properties = tree.getPropertyPointer("S"); 
   MemoryBlock memblock(properties, 64*90); // Again, maybe this is crazy too,
                                   // but I cannot figure myself how to do it
                  

   int32 index = 0;
   for (int8 i=0; i<64; ++i)
   {
      Car* const currentCar = m_cars.getUnchecked(i);
      for (int8 j=0; j<90; ++j)
      {
         const int8 xx = memblock[index];
         currentCar->setProperty(j, xx);
         ++index;
      }
   }

```

While the writting to the memory Block is much faster than saving a ValueTree with lots of properties and child, it crashes with the loading code [in memcpy.asm], and I cannot figure the reason. Please, could you help me?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 28, 2016, 6:50pm UTC](https://forum.juce.com/t/help-with-memoryblock-and-valuetree/17884/2 "2016-05-28T18:50:51Z")

</div>

Maybe try using MemoryOutputStream/MemoryInputStream?

---

<div class="post-metadata">

**Author:** ![juan1979](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@juan1979](https://forum.juce.com/u/juan1979)\
**Post date:** [May 28, 2016, 7:00pm UTC](https://forum.juce.com/t/help-with-memoryblock-and-valuetree/17884/3 "2016-05-28T19:00:04Z")

</div>

Yes, I use that once I have the ValueTree completed, with all the MemoryBlocks inside.

I found the mistake: I was forgotting to use var::getBinaryData() to obtain the MemoryBlock. These lines are wrong. Wow, I spend the entire day to find the mistake! 😛

```
const var* const properties = tree.getPropertyPointer("S"); 
MemoryBlock* memblockp = properties->getBinaryData();
MemoryBlock& mbl = *memblockp;

```

Thank you for the reply anyway! 🙂
