Juce crashes while parsing large JSON


#1

I am trying to switch to use Juce's JSON library and ran into an issue when running juce::JSON::toString(myVarrayVar) for a fairly large JSON var, but nothing very fancy. The issue has something to do with memory allocation and retreival. My code looks like the below. I am keeping a juce::var member and building it up as a var array so it can later be inserted into another var.

struct QACaseResults
{
    QACaseResults()
    {
        qaCallbackResults = juce::var();
    }

    void QACaseResults::add(const juce::var& callbackResult)
    {
        
        // Works here, first insertion.
        juce::String foo = juce::JSON::toString(callbackResult, false);

        qaCallbackResults.insert(qaCallbackResults.size(), qaCallbackResults);


        // Fails here, first insertion.
        foo = juce::JSON::toString(qaCallbackResults, false);
    }

    juce::var qaCallbackResults;
};

The conversion to String works fine when converting the var that is not yet an array, but when it is converted to an array it fails. The only difference should be added brackets, so I am not sure why it crashes. Here os the stack trace:

mQATool.exe!juce::HeapBlock<char,0>::operator void *() Line 169    C++
mQATool.exe!juce::MemoryBlock::getData() Line 96    C++
mQATool.exe!juce::MemoryOutputStream::prepareToWrite(unsigned int numBytes) Line 92    C++
mQATool.exe!juce::MemoryOutputStream::write(const void * buffer, unsigned int howMany) Line 115    C++
mQATool.exe!juce::OutputStream::writeByte(char byte) Line 78    C++
mQATool.exe!juce::operator<<(juce::OutputStream & stream, char character) Line 315    C++
mQATool.exe!juce::JSONFormatter::writeArray(juce::OutputStream & out, const juce::Array<juce::var,juce::DummyCriticalSection,0> & array, const int indentLevel, const bool allOnOneLine) Line 433    C++
mQATool.exe!juce::JSONFormatter::write(juce::OutputStream & out, const juce::var & v, const int indentLevel, const bool allOnOneLine) Line 354    C++
mQATool.exe!juce::JSONFormatter::writeArray(juce::OutputStream & out, const juce::Array<juce::var,juce::DummyCriticalSection,0> & array, const int indentLevel, const bool allOnOneLine) Line 443    C++
mQATool.exe!juce::JSONFormatter::write(juce::OutputStream & out, const juce::var & v, const int indentLevel, const bool allOnOneLine) Line 354    C++
mQATool.exe!juce::JSONFormatter::writeArray(juce::OutputStream & out, const juce::Array<juce::var,juce::DummyCriticalSection,0> & array, const int indentLevel, const bool allOnOneLine) Line 443    C++
mQATool.exe!juce::JSONFormatter::write(juce::OutputStream & out, const juce::var & v, const int indentLevel, const bool allOnOneLine) Line 354    C++
mQATool.exe!juce::JSONFormatter::writeArray(juce::OutputStream & out, const juce::Array<juce::var,juce::DummyCriticalSection,0> & array, const int indentLevel, const bool allOnOneLine) Line 443    C++
...

So the code jumps back and forth between JSONFormatter::write() and JSONFormatter::writeArray() which seems to be the expected behavior, as this also happens when converting smaller (array) vars to String, except that it is parsed correctly. The data when it crashes is as follows:

- size_t numBytes/howMany  --> 1
- const void* const buffer       --> 0x08de3184
- char byte                              --> 91 '['

I have pasted my JSON here: http://pastebin.com/Y4SKpiEj

The long line is an encoded png image in raw text, nothing fancy. It is a proper JSON and I have parsed it back and forth previously using jsoncpp, so it should be no problem with the JSON itself. What can be the issue here? Am I doing something wrong?


#2

Um, there is something fishy going on. The OutputStream in juce_OutputStream.cpp, line 315 is just a bunch of '[' and indentations, like this:

[
  [
    [
      [
        [
          [
            [
              [
                [
                  [
                    [
                      [
                        [
                          [
                            [
                              [
                                [
                                  [

Maybe I am not doing this the way it is supposed to be done?


#3

Well, there you have it for not naming your variables different enough. I am adding the var qaCallbackResults to itself, hence the recursive nature of everything. I'll just leave it here in case someone else does similar stupid things.


#4

Thanks for posting the solution! That does indeed explain the error.

If you have any other issues, please let us know.