cmd("cut") and cmd("copy") crashed MS when the selection is made with score.selection.selectRange

• Sep 24, 2021 - 20:38

Hi,
I'm trying to use cmd("cut") (and cmd("cut") ) in a plugin:
* When used on user-made selection, it works
* When performed after a selection made with score.selection.select(someElement); it works (°)
* When performed after a selection made with score.selection.selectRange(...) it crashed MS (°°).
Any idea why ?

(°):

var score = curScore;
var cursor = score.newCursor();
cursor.rewind(0);
selectCursor(cursor);
cmd("cut");

function selectCursor(cursor) {
    var el = cursor.element;
    if (el.type === Element.CHORD)
        el = el.notes[0];
    else if (el.type !== Element.REST)
        return false;
    cursor.score.selection.select(el);

}

(°°):

var score = curScore;
var cursor = score.newCursor();
cursor.rewind(0);
selectMeasure(cursor);
cmd("cut");

function selectMeasure(cursor) {
    var measure = cursor.measure;
    var first = cursor.segment.nextInMeasure // build selection from the next element
    //var last=measure.lastSegment;  // <-- KO with this definition of last, a cmd("cut") crashed MS

    var last = first;
    var seg = first;
    while (seg != null) {

        console.log(seg.segmentType + " - " + seg.tick);
        if (seg.segmentType == Segment.ChordRest) {
            last = seg;
            console.log(first.tick + "/" + last.tick);
        }
        seg = seg.nextInMeasure;
    }

    cursor.score.selection.selectRange(first.tick, last.tick , cursor.staffIdx, cursor.staffIdx);  //<-- KO: the selection does not contain last chord and crashes MS
    //cursor.score.selection.selectRange(first.tick, last.nextInMeasure.tick, cursor.staffIdx, cursor.staffIdx); // <-- KO: a cmd("cut") crashed MS
    //cursor.score.selection.selectRange(first.tick, last.nextInMeasure.tick - 1, cursor.staffIdx, cursor.staffIdx); // <-- KO: a cmd("cut") crashed MS
}


Comments

Yep, log this as an issue I can reproduce relatively easily, it's crashing in
Ms::Selection::canCopy( ), the _startSegment of the selection is a null pointer when the selection has been set via the selectRange(startTick, endTick, ...) function.'
Seems you need to call score->endCmd() after that selectRange function for the selection's start element to set properly.

Do you still have an unanswered question? Please log in first to post your question.