Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/engraving/dom/instrchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ InstrumentChange::~InstrumentChange()
delete m_instrument;
}

void InstrumentChange::setInstrument(Instrument* i)
{
if (m_instrument != i) {
delete m_instrument;
m_instrument = i;
}
}

void InstrumentChange::setInstrument(const Instrument& i)
{
*m_instrument = i;
Expand Down
2 changes: 1 addition & 1 deletion src/engraving/dom/instrchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class InstrumentChange final : public TextBase
InstrumentChange* clone() const override { return new InstrumentChange(*this); }

Instrument* instrument() const { return m_instrument; }
void setInstrument(Instrument* i) { m_instrument = i; }
void setInstrument(Instrument* i);
void setInstrument(Instrument&& i) { *m_instrument = i; }
void setInstrument(const Instrument& i);
void setupInstrument(const Instrument* instrument);
Expand Down
5 changes: 4 additions & 1 deletion src/engraving/dom/instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,10 @@ Instrument* InstrumentList::instrument(int tick)

void InstrumentList::setInstrument(Instrument* instr, int tick)
{
if (!insert({ tick, instr }).second) {
auto result = insert({ tick, instr });
if (!result.second) {
// Key already exists, delete old instrument and replace
delete (*this)[tick];
(*this)[tick] = instr;
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/engraving/dom/part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ Part::Part(Score* s)
m_preferSharpFlat = PreferSharpFlat::AUTO;
}

//---------------------------------------------------------
// ~Part
//---------------------------------------------------------

Part::~Part()
{
// Delete all instruments owned by this Part
for (auto it = m_instruments.begin(); it != m_instruments.end(); ++it) {
delete it->second;
}
}

//---------------------------------------------------------
// initFromInstrTemplate
//---------------------------------------------------------
Expand Down Expand Up @@ -349,6 +361,10 @@ void Part::setInstrument(const Instrument& i, Fraction tick)

void Part::setInstruments(const InstrumentList& instruments)
{
// Delete all old instruments before clearing
for (auto it = m_instruments.begin(); it != m_instruments.end(); ++it) {
delete it->second;
}
m_instruments.clear();

for (auto it = instruments.begin(); it != instruments.end(); ++it) {
Expand All @@ -367,6 +383,7 @@ void Part::removeInstrument(const Fraction& tick)
LOGD("Part::removeInstrument: not found at tick %d", tick.ticks());
return;
}
delete i->second; // Delete the instrument before erasing
m_instruments.erase(i);
}

Expand All @@ -379,6 +396,7 @@ void Part::removeNonPrimaryInstruments()
auto it = m_instruments.begin();
while (it != m_instruments.end()) {
if (it->first != -1) {
delete it->second; // Delete the instrument before erasing
it = m_instruments.erase(it);
continue;
}
Expand Down
1 change: 1 addition & 0 deletions src/engraving/dom/part.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Part final : public EngravingObject
static const int DEFAULT_COLOR = 0x3399ff;

Part(Score* score = nullptr);
~Part();
void initFromInstrTemplate(const InstrumentTemplate*);

const muse::ID& id() const;
Expand Down
16 changes: 16 additions & 0 deletions src/engraving/editing/editinstrumentchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,19 @@
// remember original instrument
instrument = oi;
}

//---------------------------------------------------------
// ChangeInstrument::cleanup
//---------------------------------------------------------

void ChangeInstrument::cleanup(bool undo)

Check warning on line 63 in src/engraving/editing/editinstrumentchange.cpp

View workflow job for this annotation

GitHub Actions / windows_x64

'undo': unreferenced parameter

Check warning on line 63 in src/engraving/editing/editinstrumentchange.cpp

View workflow job for this annotation

GitHub Actions / build (linux_x64)

unused parameter ‘undo’ [-Wunused-parameter]

Check warning on line 63 in src/engraving/editing/editinstrumentchange.cpp

View workflow job for this annotation

GitHub Actions / build (linux_arm64)

unused parameter ‘undo’ [-Wunused-parameter]
{
// Delete the instrument pointer that is not currently in use
// After undo/redo, one of the instruments is no longer referenced
// The 'instrument' member holds the "other" instrument after flip
// We need to delete it when the undo command is being cleaned up
if (instrument) {
delete instrument;
instrument = nullptr;
}
}
1 change: 1 addition & 0 deletions src/engraving/editing/editinstrumentchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ChangeInstrument : public UndoCommand
Instrument* instrument = nullptr;

void flip(EditData*) override;
void cleanup(bool undo) override;

public:
ChangeInstrument(InstrumentChange* _is, Instrument* i)
Expand Down
Loading