Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed memory leak #668

Merged
merged 12 commits into from
Sep 28, 2023
37 changes: 20 additions & 17 deletions ESPixelStick/src/FileMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ void c_FileMgr::InitSdFileList ()
int index = 0;
for (auto& currentFileListEntry : FileList)
{
currentFileListEntry.handle = 0;
currentFileListEntry.handle = INVALID_FILE_HANDLE;
currentFileListEntry.entryId = index++;
}

Expand All @@ -692,15 +692,18 @@ int c_FileMgr::FileListFindSdFileHandle (FileId HandleToFind)
int response = -1;
// DEBUG_V (String ("HandleToFind: ") + String (HandleToFind));

for (auto & currentFileListEntry : FileList)
if(INVALID_FILE_HANDLE != HandleToFind)
{
// DEBUG_V (String ("currentFileListEntry.handle: ") + String (currentFileListEntry.handle));
// DEBUG_V (String ("currentFileListEntry.entryId: ") + String (currentFileListEntry.entryId));

if (currentFileListEntry.handle == HandleToFind)
for (auto & currentFileListEntry : FileList)
{
response = currentFileListEntry.entryId;
break;
// DEBUG_V (String ("currentFileListEntry.handle: ") + String (currentFileListEntry.handle));
// DEBUG_V (String ("currentFileListEntry.entryId: ") + String (currentFileListEntry.entryId));

if (currentFileListEntry.handle == HandleToFind)
{
response = currentFileListEntry.entryId;
break;
}
}
}

Expand All @@ -714,7 +717,7 @@ c_FileMgr::FileId c_FileMgr::CreateSdFileHandle ()
{
// DEBUG_START;

FileId response = 0;
FileId response = INVALID_FILE_HANDLE;
FileId FileHandle = millis ();

// create a unique handle
Expand All @@ -726,15 +729,15 @@ c_FileMgr::FileId c_FileMgr::CreateSdFileHandle ()
// find an empty slot
for (auto & currentFileListEntry : FileList)
{
if (currentFileListEntry.handle == 0)
if (currentFileListEntry.handle == INVALID_FILE_HANDLE)
{
currentFileListEntry.handle = FileHandle;
response = FileHandle;
break;
}
}

if (0 == response)
if (INVALID_FILE_HANDLE == response)
{
logcon (String (CN_stars) + F (" Could not allocate another file handle ") + CN_stars);
}
Expand Down Expand Up @@ -968,7 +971,7 @@ void c_FileMgr::SaveSdFile (const String & FileName, String & FileData)

do // once
{
FileId FileHandle = 0;
FileId FileHandle = INVALID_FILE_HANDLE;
if (false == OpenSdFile (FileName, FileMode::FileWrite, FileHandle))
{
logcon (String (F ("Could not open '")) + FileName + F ("' for writting."));
Expand Down Expand Up @@ -1047,7 +1050,7 @@ bool c_FileMgr::OpenSdFile (const String & FileName, FileMode Mode, FileId & Fil
logcon(String(F("ERROR: Cannot open '")) + FileName + F("'."));

// release the file list entry
FileList[FileListIndex].handle = 0;
FileList[FileListIndex].handle = INVALID_FILE_HANDLE;

break;
}
Expand Down Expand Up @@ -1079,7 +1082,7 @@ bool c_FileMgr::ReadSdFile (const String & FileName, String & FileData)
// DEBUG_START;

bool GotFileData = false;
FileId FileHandle = 0;
FileId FileHandle = INVALID_FILE_HANDLE;

// DEBUG_V (String("File '") + FileName + "' is being opened.");
if (true == OpenSdFile (FileName, FileMode::FileRead, FileHandle))
Expand Down Expand Up @@ -1115,7 +1118,7 @@ bool c_FileMgr::ReadSdFile (const String & FileName, JsonDocument & FileData)
// DEBUG_START;

bool GotFileData = false;
FileId FileHandle = 0;
FileId FileHandle = INVALID_FILE_HANDLE;

// DEBUG_V (String("File '") + FileName + "' is being opened.");
if (true == OpenSdFile (FileName, FileMode::FileRead, FileHandle))
Expand Down Expand Up @@ -1232,7 +1235,7 @@ void c_FileMgr::CloseSdFile (const FileId& FileHandle)
if (-1 != (FileListIndex = FileListFindSdFileHandle (FileHandle)))
{
FileList[FileListIndex].info.close ();
FileList[FileListIndex].handle = 0;
FileList[FileListIndex].handle = INVALID_FILE_HANDLE;
}
else
{
Expand Down Expand Up @@ -1286,7 +1289,7 @@ size_t c_FileMgr::WriteSdFile (const FileId& FileHandle, byte* FileData, size_t
size_t c_FileMgr::GetSdFileSize (const String& FileName)
{
size_t response = 0;
FileId Handle;
FileId Handle = INVALID_FILE_HANDLE;
if(OpenSdFile (FileName, FileMode::FileRead, Handle))
{
response = GetSdFileSize(Handle);
Expand Down
1 change: 1 addition & 0 deletions ESPixelStick/src/FileMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class c_FileMgr
virtual ~c_FileMgr ();

typedef uint32_t FileId;
const static FileId INVALID_FILE_HANDLE = 0;

void Begin ();
void Poll () {}
Expand Down
2 changes: 1 addition & 1 deletion ESPixelStick/src/input/InputFPPRemotePlayEffectFsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void fsm_PlayEffect_state_PlayingEffect::Poll ()
if (p_InputFPPRemotePlayEffect->PlayEffectTimer.IsExpired())
{
// DEBUG_V ("");
Stop ();
p_InputFPPRemotePlayEffect->Stop ();
}

// DEBUG_END;
Expand Down
12 changes: 9 additions & 3 deletions ESPixelStick/src/input/InputFPPRemotePlayFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ bool c_InputFPPRemotePlayFile::ParseFseqFile ()
FSEQRawHeader fsqRawHeader;
FSEQParsedHeader fsqParsedHeader;

FileHandleForFileBeingPlayed = -1;
if(c_FileMgr::INVALID_FILE_HANDLE != FileHandleForFileBeingPlayed)
{
// DEBUG_V("Unexpected File Handle at fseq Parse.");
Stop();
}

if (false == FileMgr.OpenSdFile (PlayItemName,
c_FileMgr::FileMode::FileRead,
FileHandleForFileBeingPlayed))
Expand Down Expand Up @@ -459,15 +464,16 @@ bool c_InputFPPRemotePlayFile::ParseFseqFile ()
//-----------------------------------------------------------------------------
void c_InputFPPRemotePlayFile::ClearFileInfo()
{
PlayItemName = String ("");
// DEBUG_START;
PlayItemName = emptyString;
RemainingPlayCount = 0;
SyncControl.LastRcvdElapsedSeconds = 0.0;
FrameControl.ElapsedPlayTimeMS = 0;
FrameControl.DataOffset = 0;
FrameControl.ChannelsPerFrame = 0;
FrameControl.FrameStepTimeMS = 25;
FrameControl.TotalNumberOfFramesInSequence = 0;

// DEBUG_END;
} // ClearFileInfo

uint32_t c_InputFPPRemotePlayFile::ReadFile(uint32_t DestinationIntensityId, uint32_t NumBytesToRead, uint32_t FileOffset)
Expand Down
2 changes: 1 addition & 1 deletion ESPixelStick/src/input/InputFPPRemotePlayFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class c_InputFPPRemotePlayFile : public c_InputFPPRemotePlayItem

fsm_PlayFile_state * pCurrentFsmState = &fsm_PlayFile_state_Idle_imp;

c_FileMgr::FileId FileHandleForFileBeingPlayed = 0;
c_FileMgr::FileId FileHandleForFileBeingPlayed = c_FileMgr::INVALID_FILE_HANDLE;

struct FrameControl_t
{
Expand Down
19 changes: 8 additions & 11 deletions ESPixelStick/src/input/InputFPPRemotePlayFileFsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void fsm_PlayFile_state_PlayingFile::Poll ()
{
// DEBUG_V (String ("TotalNumberOfFramesInSequence: ") + String (p_Parent->TotalNumberOfFramesInSequence));
// DEBUG_V (String (" Done Playing:: FileName: '") + p_Parent->GetFileName () + "'");
Stop ();
p_Parent->Stop ();
break;
}
}
Expand Down Expand Up @@ -284,11 +284,8 @@ IRAM_ATTR void fsm_PlayFile_state_PlayingFile::TimerPoll ()
// xDEBUG_V (String ("TotalNumberOfFramesInSequence: ") + String (p_Parent->TotalNumberOfFramesInSequence));
// xDEBUG_V (String (" CurrentFrame: ") + String (CurrentFrame));

if (0 != p_Parent->FileHandleForFileBeingPlayed)
{
// logcon (F ("File Playback Failed to read enough data"));
Stop ();
}
// logcon (F ("File Playback Failed to read enough data"));
p_Parent->Stop ();
}
}

Expand Down Expand Up @@ -319,7 +316,7 @@ void fsm_PlayFile_state_PlayingFile::Init (c_InputFPPRemotePlayFile* Parent)
// DEBUG_V (String ("RemainingPlayCount: ") + p_Parent->RemainingPlayCount);
if (0 == p_Parent->RemainingPlayCount)
{
Stop ();
p_Parent->Stop ();
break;
}

Expand Down Expand Up @@ -358,7 +355,7 @@ void fsm_PlayFile_state_PlayingFile::Start (String& FileName, float ElapsedSecon
// DEBUG_V (String ("TotalNumberOfFramesInSequence: ") + String (p_Parent->TotalNumberOfFramesInSequence));
// DEBUG_V (String ("RemainingPlayCount: ") + p_Parent->RemainingPlayCount);

Stop ();
p_Parent->Stop ();
p_Parent->Start (FileName, ElapsedSeconds, PlayCount);

// DEBUG_END;
Expand Down Expand Up @@ -396,7 +393,7 @@ bool fsm_PlayFile_state_PlayingFile::Sync (String& FileName, float ElapsedSecond
if (FileName != p_Parent->GetFileName ())
{
// DEBUG_V ("Sync: Filename change");
Stop ();
p_Parent->Stop ();
p_Parent->Start (FileName, ElapsedSeconds, 1);
break;
}
Expand Down Expand Up @@ -457,10 +454,10 @@ void fsm_PlayFile_state_Stopping::Poll ()
// DEBUG_V (String ("FileHandleForFileBeingPlayed: ") + String (p_Parent->FileHandleForFileBeingPlayed));

FileMgr.CloseSdFile (p_Parent->FileHandleForFileBeingPlayed);
p_Parent->FileHandleForFileBeingPlayed = 0;
p_Parent->FileHandleForFileBeingPlayed = c_FileMgr::INVALID_FILE_HANDLE;
p_Parent->fsm_PlayFile_state_Idle_imp.Init (p_Parent);

if (FileName != "")
if (!FileName.equals(emptyString))
{
// DEBUG_V ("Restarting File");
p_Parent->Start (FileName, StartingElapsedTime, PlayCount);
Expand Down
6 changes: 3 additions & 3 deletions ESPixelStick/src/input/InputFPPRemotePlayListFsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void fsm_PlayList_state_PlayingFile::Poll ()
if (pInputFPPRemotePlayList->pInputFPPRemotePlayItem->IsIdle ())
{
// DEBUG_V ("Done with all entries");
Stop ();
pInputFPPRemotePlayList->Stop ();
}

// DEBUG_END;
Expand Down Expand Up @@ -236,7 +236,7 @@ void fsm_PlayList_state_PlayingEffect::Poll ()
if (pInputFPPRemotePlayList->pInputFPPRemotePlayItem->IsIdle ())
{
// DEBUG_V ("Effect Processing Done");
Stop ();
pInputFPPRemotePlayList->Stop ();
}

// DEBUG_END;
Expand Down Expand Up @@ -311,7 +311,7 @@ void fsm_PlayList_state_Paused::Poll ()

if (pInputFPPRemotePlayList->PauseDelayTimer.IsExpired())
{
Stop();
pInputFPPRemotePlayList->Stop();
}

// DEBUG_END;
Expand Down