MaxobjListener issue

fraction's icon

Initially, I believed it might be a Max for Live issue, but I'm not entirely sure whether it's an anormal behavior or just the way things are supposed to work.

Let's focus on the behavior of the maxobjlistener in a js object. Is it expected that they get reset or broke whenever you delete another js object or close a patch that contains a js object, even if the code within it is empty?

To better understand the situation and try it, please open the attached patch and follow the notes provided in the UI. I apologize in advance if this behavior is considered normal, but if it is, I think it could pose a problem, especially in Ableton where all your listeners are affected whenever you delete a device containing a js object.

thanks in advance for the help! :)
I m on max 8.55 / mac OS.

mytest.maxpat
Max Patch

mytest.js
js 0.71 KB

tyler mazaika's icon

That is weird behavior! However, I think you want to declare your group1maxobj / group2maxobj outside of the bang() function. When I change that up they behave as I think you expect.

autowatch = 1;
inlets = 1;
outlets = 1;
var thisP = this.patcher;

// Define the names of the live.text objects
var liveTextGroup = ["Percep_group1", "Percep_group2"];

var group1maxobj; /////////// Moved out here
var group2maxobj;

function bang() {
    var group1Status = this.patcher.getnamed("Percep_group1");
group1maxobj = new MaxobjListener (group1Status, group1Valuechanged);

    var group2Status = this.patcher.getnamed("Percep_group2");
group2maxobj = new MaxobjListener (group2Status, group2valuechanged);
}

function group1Valuechanged(mydata1){;

var currentgroup1status = mydata1.value
if (currentgroup1status == 1) { post("yes1\n")}
    }
    
function group2valuechanged(mydata2){;

var currentgroup2status = mydata2.value
if (currentgroup2status == 1) { post("yes2\n")}
    }

My hunch would be that when they're declared in the bang() they hang around until something triggers garbage collection automatically so they seem like everything is okay for a while -- and then they are not.

fraction's icon

thanks a lot. this is really un intuitive - or rather i dont understand at all the link with adding another js object (empty) and delete it. That's super odd, isn'it?
I m going to try with that furthermore,

thx again! :)

fraction's icon

finally i also changed few stuffs, because my liveTextGroup array has more items, therefore more callback function. It seems to work. I would still enjoy to understand the reason of the previous behavior.

var liveTextGroup = ["Percep_group1", "Percep_group2", "Percep_group3", "Percep_group4", "Percep_group5"];

// Array to store MaxobjListener objects
var listeners = [];

function bang() {
for (var i = 0; i < liveTextGroup.length; i++) {
var obj = p.getnamed(liveTextGroup[i]);
listeners.push(new MaxobjListener(obj, getLiveTextValueChangeCallback(i)));
}
}

..if that can be useful to anyone!