cwPresetSel.cpp : Fix problem where there were two version of delete_fragment() - one of which allowed a memory leak.

This commit is contained in:
kevin 2024-03-27 20:19:06 -04:00
parent 89a4b83c45
commit 73b433feae

View File

@ -105,33 +105,47 @@ namespace cw
rc_t _delete_fragment( preset_sel_t* p, unsigned fragId ) rc_t _delete_fragment( preset_sel_t* p, unsigned fragId )
{ {
frag_t* f0 = nullptr; rc_t rc = kOkRC;
frag_t* f1 = p->fragL; frag_t* f = p->fragL;
for(; f1!=nullptr; f1=f1->link) for(; f!=nullptr; f=f->link)
{ {
if( f1->fragId == fragId ) if( f->fragId == fragId )
{ {
if( f0 == nullptr ) // if this is the first frag in the list
p->fragL = f1->link; if( f->prev == nullptr )
p->fragL = f->link;
else else
f0->link = f1->link; {
for(unsigned i=0; i<f1->presetN; ++i) // link the prev fragment to the next fragment
mem::release(f1->presetA[i].alt_str); f->prev->link = f->link;
// dur of prev frag now include the dur of the deleted frag
f->prev->endLoc = f->endLoc;
}
// link the next fragment back to the previous fragment
if( f->link != nullptr )
f->link->prev = f->prev;
for(unsigned i=0; i<f->presetN; ++i)
mem::release(f->presetA[i].alt_str);
// release the fragment // release the fragment
mem::release(f1->note); mem::release(f->note);
mem::release(f1->presetA); mem::release(f->presetA);
mem::release(f1); mem::release(f->altPresetIdxA);
mem::release(f->multiPresetA);
return kOkRC; mem::release(f);
goto errLabel;
}
} }
f0 = f1; rc = cwLogError(kEleNotFoundRC,"The fragment with id %i was not found.",fragId);
}
return kOkRC; errLabel:
return rc;
} }
void _destroy_all_frags( preset_sel_t* p ) void _destroy_all_frags( preset_sel_t* p )
@ -242,7 +256,7 @@ namespace cw
else else
{ {
unsigned alt_strN = textLength(alt_str); unsigned alt_strN = textLength(alt_str);
char alt_str_buf[ alt_strN+1 ] = {0}; char alt_str_buf[ alt_strN+1 ];
unsigned asi = 0; unsigned asi = 0;
// clear the alt's pointing to the selected preset - because the 'alt_str' has changed // clear the alt's pointing to the selected preset - because the 'alt_str' has changed
@ -258,6 +272,9 @@ namespace cw
alt_str_buf[ asi++ ] = alt_str[i]; alt_str_buf[ asi++ ] = alt_str[i];
} }
assert( asi <= alt_strN );
alt_str_buf[asi] = 0;
// store the preset's new alt str. // store the preset's new alt str.
f->presetA[ sel_preset_idx ].alt_str = mem::reallocStr(f->presetA[ sel_preset_idx ].alt_str, alt_str_buf); f->presetA[ sel_preset_idx ].alt_str = mem::reallocStr(f->presetA[ sel_preset_idx ].alt_str, alt_str_buf);
} }
@ -1145,33 +1162,7 @@ cw::rc_t cw::preset_sel::create_fragment( handle_t h, unsigned end_loc, time::sp
cw::rc_t cw::preset_sel::delete_fragment( handle_t h, unsigned fragId ) cw::rc_t cw::preset_sel::delete_fragment( handle_t h, unsigned fragId )
{ {
preset_sel_t* p = _handleToPtr(h); preset_sel_t* p = _handleToPtr(h);
frag_t* f = p->fragL; return _delete_fragment(p,fragId);
for(; f!=nullptr; f=f->link)
if( f->fragId == fragId )
{
if( f->prev == nullptr )
p->fragL = f->link;
else
{
// the previous fragment's end-loc become the
// endloc of the deleted fragment
f->prev->endLoc = f->endLoc;
f->prev->link = f->link;
}
if( f->link != nullptr )
f->link->prev = f->prev;
// release the fragment
mem::release(f->presetA);
mem::release(f->multiPresetA);
mem::release(f);
return kOkRC;
}
return cwLogError(kInvalidArgRC,"The fragment '%i' could not be found to delete.",fragId);
} }
bool cw::preset_sel::is_fragment_end_loc( handle_t h, unsigned loc ) bool cw::preset_sel::is_fragment_end_loc( handle_t h, unsigned loc )