Working with Strings in getStateInformation() and setStateInformation


Maybe I’m missing something obvious, but I couldn’t find any method to directly copy a String into a MemoryBlock, so I could give a human-readable string description of my plugin’s settings in AudioProcessor::getStateInformation(). I don’t want to use any base 64 encoding or anything like that.

Thanks a lot in advance for your hints and solutions,

Before switching to XML I just used to put a length byte, followed by the string contents so it was trivial to write a small helper function. I switched everything to work with the XML classes recently and working with saving/restoring state is much easier

See this thread:


You could also just wrap a MemoryOutputStream around your MemoryBlock, and then just write the string into it.

But like leehu said, XML is the smart thing to use here, it means that in future you can just add properties without changing your entire format.

This is the bit I use for just the string writing part:

void FilterProcessor::getStateInformation (MemoryBlock& destData)
    String myString;
    MemoryOutputStream stream(destData, false);
    stream.writeText (myString, true, true);
void FilterProcessor::setStateInformation (const void* data, int sizeInBytes)
    String myString = String::createStringFromData (data, sizeInBytes);


1 Like

Thanks a lot for your replies. In the meanwhile I found a solution which was the kind of straightforward solution I was looking for. It seems to work fine, but please let me know if there’s something wrong with it:
destData.append(settings.toRawUTF8(), CharPointer_UTF8::getBytesRequiredFor(settings.getCharPointer()));

Maybe use rather String::getNumBytesAsUTF8() than getBytesRequired(), but apart from that looks good to me…


@jules etc Why even use a memory block in the method signature? Why not just require straight XML?

Because I used to put JSON into there :stuck_out_tongue:
Just because I send data back and forth to the rest of the team, who happen to use node.js

And even in my personal project, I use ValueTree’s binary read and write methods…

so MemoryBlock is the lowest common denominator among JSON/Strings/XML?

1 Like

I think it is just a virtual file representation, where you can use any write method. And it has no size limitations too…
The opposite way can use a raw pointer and size, because the size is already known.