From 8ae846ee05c1520bc90c4ef7214ddb5c8a089d6d Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 30 Apr 2023 21:18:38 -0400 Subject: [PATCH] cwMidiFile.h/cpp : Fixed memory leaks. --- cwMidiFile.cpp | 82 ++++++++++++++++++++++++++++++++++++++++---------- cwMidiFile.h | 5 +-- 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/cwMidiFile.cpp b/cwMidiFile.cpp index 6127ee8..36597f2 100644 --- a/cwMidiFile.cpp +++ b/cwMidiFile.cpp @@ -110,11 +110,20 @@ namespace cw char* t = mem::allocZ(byteN+1); t[byteN] = 0; - if((rc = cw::file::readChar(mfp->fh,t,byteN)) != kOkRC ) - return cwLogError(rc,"MIDI read text failed."); - + if((rc = cw::file::readChar(mfp->fh,t,byteN)) != kOkRC ) + { + rc = cwLogError(rc,"MIDI read text failed."); + goto errLabel; + } + tmp->u.text = t; tmp->byteCnt = byteN; + tmp->flags |= kReleaseFl; + + errLabel: + if( rc != kOkRC ) + mem::release(t); + return rc; } @@ -123,12 +132,20 @@ namespace cw rc_t rc = kOkRC; char* t = mem::allocZ(byteN); - if((rc = cw::file::readChar(mfp->fh,t,byteN)) != kOkRC ) - return cwLogError(rc,"MIDI read record failed."); - + if((rc = cw::file::readChar(mfp->fh,t,byteN)) != kOkRC ) + { + rc = cwLogError(rc,"MIDI read record failed."); + goto errLabel; + } + tmp->byteCnt = byteN; tmp->u.voidPtr = t; + tmp->flags |= kReleaseFl; + errLabel: + if( rc != kOkRC ) + mem::release(t); + return rc; } @@ -170,7 +187,7 @@ namespace cw tmp->trkIdx = trkIdx; tmp->byteCnt = 0; tmp->uid = mfp->nextUid++; - + return tmp; } @@ -230,11 +247,19 @@ namespace cw uint8_t* mp = mem::allocZ( byteN ); // read the sys-ex msg from the file into msg memory - if((rc = cw::file::readUChar(mfp->fh,mp,byteN)) != kOkRC ) - return cwLogError(rc,"MIDI sys-ex read failed."); - + if((rc = cw::file::readUChar(mfp->fh,mp,byteN)) != kOkRC ) + { + rc = cwLogError(rc,"MIDI sys-ex read failed."); + goto errLabel; + } + tmp->byteCnt = byteN; - tmp->u.sysExPtr = mp; + tmp->u.sysExPtr = mp; + tmp->flags |= kReleaseFl; + + errLabel: + if( rc != kOkRC ) + mem::release(mp); return rc; } @@ -259,15 +284,18 @@ namespace cw unsigned byteN = statusToByteCount(tmp->status); if( byteN==kInvalidMidiByte || byteN > 2 ) - return cwLogError(kSyntaxErrorRC,"Invalid status:0x%x %i byte cnt:%i.",tmp->status,tmp->status,byteN); - + { + rc = cwLogError(kSyntaxErrorRC,"Invalid status:0x%x %i byte cnt:%i.",tmp->status,tmp->status,byteN); + goto errLabel; + } + unsigned i; for(i=useRsFl; id0 : &p->d1; if((rc = _read8(mfp,b)) != kOkRC ) - return rc; + goto errLabel; } // convert note-on velocity=0 to note off @@ -275,7 +303,12 @@ namespace cw tmp->status = kNoteOffMdId; tmp->u.chMsgPtr = p; - + tmp->flags |= kReleaseFl; + + errLabel: + if( rc != kOkRC ) + mem::release(p); + return rc; } @@ -545,12 +578,28 @@ namespace cw if( mfp == NULL ) return rc; + for(unsigned i=0; itrkN; ++i) + { + trackMsg_t* t = mfp->trkV[i].base; + while( t!=nullptr ) + { + trackMsg_t* t0 = t->link; + + if( cwIsFlag(t->flags,kReleaseFl) ) + mem::release((void*&)(t->u.voidPtr)); + + mem::release(t); + t = t0; + } + } + mem::release(mfp->trkV); mem::release(mfp->msgV); if( mfp->fh.isValid() ) if((rc = cw::file::close( mfp->fh )) != kOkRC ) rc = cwLogError(rc,"MIDI file close failed."); - + + mem::release(mfp->fn); mem::release(mfp); return rc; @@ -1484,6 +1533,7 @@ cw::rc_t cw::midi::file::insertMsg( handle_t h, unsigned uid, int dtick, uint8_t chMsg_t* c = mem::allocZ(); m->u.chMsgPtr = c; + m->flags |= kReleaseFl; c->ch = ch; c->d0 = d0; diff --git a/cwMidiFile.h b/cwMidiFile.h index df2018e..d9f86be 100644 --- a/cwMidiFile.h +++ b/cwMidiFile.h @@ -65,7 +65,8 @@ namespace cw enum { - kDropTrkMsgFl = 0x01 + kDropTrkMsgFl = 0x01, + kReleaseFl = 0x02, }; typedef struct trackMsg_str @@ -209,7 +210,7 @@ namespace cw } density_t; // Generate the note onset density measure for each note in the MIDI file. - // Delete the returned memory with a call to cmMemFree(). + // Delete the returned memory with a call to mem::release(). density_t* noteDensity( handle_t h, unsigned* cntRef );