JUCE, JSON and leakage

Hi, I’ve recently returned to C++ after a long holiday in Javascript. I am attempting to create a JSON representation of sound sources and their mix values to be applied to each of a number of speakers, where the speakers are in different zones.

I have a few questions about the JSON construction, and would appreciate some guidelines:

  1. I create the mix values
    Array<var> speakerMixVarLevels;

    // Create a JUCE array of var values for each mix level - can be added as a property
    for (int i = 0; i < zonePtr->noSpeakers; i++) {
        speakerMixVarLevels.add(speakerMixLevels[i]);
    }

    //DynamicObject::Ptr mixLevelsObject = new DynamicObject();

    // The final return object containing the speaker mix values
    DynamicObject* mixLevelsObject = new DynamicObject();
   
    mixLevelsObject->setProperty("Zone", zonePtr->Name);
    mixLevelsObject->setProperty("Channel", channel);
    mixLevelsObject->setProperty("MixValues", speakerMixVarLevels);

    //Make the object a var type for inclusion in the mix matrix JSON
    var mixLevelsVar(mixLevelsObject);

I found that I was not able to use DynamicObject::Ptr if I wanted to make the DynamicObject to be type var. Why is this?

  1. I would like to pass back var objects to functions that do further JSON aggregation:
return mixLevelsVar;

and also pass var objects and var arrays (Array<var>) to functions:

var SoundSource::renderSoundSource(var soundObject) {

Does this pose a problem for reference counting? Should one rather pass strings between functions, and parse the strings? I have been getting leakage errors, hence my question.

Thanks!

Try

var mixLevelsVar (mixLevelsObject.get());

It shouldn’t. The objects will be copied.

If you want to create and work with JSON in C++ I recommend the nlohmann json library over juce::var for ease of use.

3 Likes

I find JUCE’s var stuff less cumbersome than something like RapidJSON, but this library looks great. There are some cool ideas in there that JUCE could have like the user-defined literal, and more importantly some kind of initializer_list parser to create things inline. This way, at least creation is more intuitive. I’m not sure about suggestions in terms of accessing/mutating…

var mixLevelsVar (mixLevelsObject.get());

Thank you very much, I will try this shortly.

Also, thanks for the confirmation about var parameter passing/returns and their lack of effect on reference counting - very helpful!

Apologies, slow with feedback - the approach you suggested plugged my leaks! Here is some code in case anyone has a similar problem. Its immersive sound code, where I determine speaker mix values for sound sources:

		// A JUCE array for each speaker mix level of the source
		for (int j = 0; j < noSpeakers; j++) {
			speakerMixVarLevels.add(mixValues[j]);
		}

		//An object which contains zone, channel and speaker mix levels for the source
		DynamicObject::Ptr mixLevelsObject = new DynamicObject();

		mixLevelsObject->setProperty("Zone", zoneName);
		mixLevelsObject->setProperty("Channel", channel);
		mixLevelsObject->setProperty("MixValues", speakerMixVarLevels);

		var mixLevelsVar(mixLevelsObject.get());
		mixMatrixObjects.add(mixLevelsVar); //Add it to the array of speaker mix levels for each source

Thank you very much!