From a2934abf73a4d0a03fe7b832b4a12475ca2236c0 Mon Sep 17 00:00:00 2001 From: zilmar Date: Thu, 22 Apr 2021 09:30:27 +0930 Subject: [PATCH] Remove usage of malloc --- .../Debugger/Debugger-Commands.cpp | 10 ++++----- .../Debugger/Debugger-MemoryDump.cpp | 8 +++---- .../Debugger/Debugger-ViewMemory.cpp | 9 +++----- .../UserInterface/Debugger/MemoryScanner.cpp | 22 +++---------------- .../UserInterface/Debugger/MemoryScanner.h | 3 ++- .../UserInterface/Debugger/ScriptSystem.h | 11 ++-------- 6 files changed, 18 insertions(+), 45 deletions(-) diff --git a/Source/Project64/UserInterface/Debugger/Debugger-Commands.cpp b/Source/Project64/UserInterface/Debugger/Debugger-Commands.cpp index f1298b075..d47edd6c0 100644 --- a/Source/Project64/UserInterface/Debugger/Debugger-Commands.cpp +++ b/Source/Project64/UserInterface/Debugger/Debugger-Commands.cpp @@ -1441,10 +1441,11 @@ LRESULT CDebugCommandsView::OnListBoxClicked(WORD /*wNotifyCode*/, WORD wID, HWN int index = m_BreakpointList.GetCaretIndex(); uint32_t address = m_BreakpointList.GetItemData(index); int len = m_BreakpointList.GetTextLen(index); - wchar_t* rowText = (wchar_t*)malloc((len + 1) * sizeof(wchar_t)); - rowText[len] = '\0'; - m_BreakpointList.GetText(index, rowText); - if (*rowText == L'E') + std::wstring rowText; + rowText.resize(len); + + m_BreakpointList.GetText(index, (wchar_t *)rowText.data()); + if (rowText[0] == L'E') { ShowAddress(address, true); } @@ -1452,7 +1453,6 @@ LRESULT CDebugCommandsView::OnListBoxClicked(WORD /*wNotifyCode*/, WORD wID, HWN { m_Debugger->Debug_ShowMemoryLocation(address, true); } - free(rowText); } return FALSE; } diff --git a/Source/Project64/UserInterface/Debugger/Debugger-MemoryDump.cpp b/Source/Project64/UserInterface/Debugger/Debugger-MemoryDump.cpp index bf4f50a0f..291bc5791 100644 --- a/Source/Project64/UserInterface/Debugger/Debugger-MemoryDump.cpp +++ b/Source/Project64/UserInterface/Debugger/Debugger-MemoryDump.cpp @@ -180,26 +180,24 @@ bool CDumpMemory::DumpMemory(LPCTSTR FileName, DumpFormat Format, DWORD StartPC, } uint32_t dumpLen = EndPC - StartPC; - uint8_t* dumpBuf = (uint8_t*)malloc(dumpLen); + std::unique_ptr dumpBuf = std::make_unique(dumpLen); uint32_t dumpIdx = 0; for (uint32_t pc = StartPC; pc < EndPC; pc++, dumpIdx++) { - bool bReadable = m_Debugger->DebugLoad_VAddr(pc, dumpBuf[dumpIdx]); + bool bReadable = m_Debugger->DebugLoad_VAddr(pc, dumpBuf.get()[dumpIdx]); if (!bReadable) { g_Notify->DisplayError(stdstr_f("Address error\n%s", strFile.c_str()).c_str()); dumpFile.Close(); - free(dumpBuf); return false; } } dumpFile.SeekToBegin(); - dumpFile.Write(dumpBuf, dumpLen); + dumpFile.Write(dumpBuf.get(), dumpLen); dumpFile.Close(); - free(dumpBuf); return true; } diff --git a/Source/Project64/UserInterface/Debugger/Debugger-ViewMemory.cpp b/Source/Project64/UserInterface/Debugger/Debugger-ViewMemory.cpp index be9a51cfb..dfc52bf58 100644 --- a/Source/Project64/UserInterface/Debugger/Debugger-ViewMemory.cpp +++ b/Source/Project64/UserInterface/Debugger/Debugger-ViewMemory.cpp @@ -972,21 +972,18 @@ LRESULT CDebugMemoryView::OnHxPaste(LPNMHDR lpNMHDR) if (nmp->column == HX_COL_HEXDATA) { - char* data = nullptr; // TODO: move this function to some utility class int length = CMemoryScanner::ParseHexString(nullptr, text); if (length != 0) { - data = (char*)malloc(length); - CMemoryScanner::ParseHexString(data, text); + std::unique_ptr data = std::make_unique(length); + CMemoryScanner::ParseHexString(data.get(), text); for (int i = 0; i < length; i++) { - SetByte(nmp->address + i, data[i]); + SetByte(nmp->address + i, data.get()[i]); } - - free(data); } retDataLength = length; diff --git a/Source/Project64/UserInterface/Debugger/MemoryScanner.cpp b/Source/Project64/UserInterface/Debugger/MemoryScanner.cpp index 50238262b..5c820dff2 100644 --- a/Source/Project64/UserInterface/Debugger/MemoryScanner.cpp +++ b/Source/Project64/UserInterface/Debugger/MemoryScanner.cpp @@ -152,33 +152,17 @@ CScanResult::~CScanResult(void) void CScanResult::SetDescription(const char* str) { - if (m_Description != nullptr) - { - free(m_Description); - } - - size_t len = strlen(str); - m_Description = (char*)malloc(len + 1); - strcpy(m_Description, str); - m_Description[len] = '\0'; + m_Description = str; } void CScanResult::DeleteDescription(void) { - if (m_Description != nullptr) - { - free(m_Description); - m_Description = nullptr; - } + m_Description.clear(); } const char* CScanResult::GetDescription(void) { - if (m_Description == nullptr) - { - return ""; - } - return m_Description; + return m_Description.c_str(); } int CScanResult::GetValueString(char *buffer, size_t size) diff --git a/Source/Project64/UserInterface/Debugger/MemoryScanner.h b/Source/Project64/UserInterface/Debugger/MemoryScanner.h index caddfe8fa..229ea3e8e 100644 --- a/Source/Project64/UserInterface/Debugger/MemoryScanner.h +++ b/Source/Project64/UserInterface/Debugger/MemoryScanner.h @@ -1,6 +1,7 @@ #pragma once #include +#include enum ValueType { @@ -137,7 +138,7 @@ public: AddressType m_AddressType; DisplayFormat m_DisplayFormat; bool m_bSelected; - char* m_Description; + std::string m_Description; public: int GetValueString(char* buffer, size_t size); diff --git a/Source/Project64/UserInterface/Debugger/ScriptSystem.h b/Source/Project64/UserInterface/Debugger/ScriptSystem.h index ef764bc7e..3fc796e4b 100644 --- a/Source/Project64/UserInterface/Debugger/ScriptSystem.h +++ b/Source/Project64/UserInterface/Debugger/ScriptSystem.h @@ -38,7 +38,7 @@ private: vector m_Hooks; vector m_RunningInstances; - vector m_LogData; + vector m_LogData; CScriptHook* m_HookCPUExec; CScriptHook* m_HookCPURead; @@ -69,16 +69,9 @@ public: return m_ScreenDC; } - inline vector* LogData() - { - return &m_LogData; - } - inline void LogText(const char* text) { - char* newStr = (char*)malloc(strlen(text)); - strcpy(newStr, text); - m_LogData.push_back(newStr); + m_LogData.push_back(text); m_Debugger->Debug_RefreshScriptsWindow(); }