Remove usage of malloc

This commit is contained in:
zilmar 2021-04-22 09:30:27 +09:30
parent 3a038b2eb3
commit a2934abf73
6 changed files with 18 additions and 45 deletions

View File

@ -1441,10 +1441,11 @@ LRESULT CDebugCommandsView::OnListBoxClicked(WORD /*wNotifyCode*/, WORD wID, HWN
int index = m_BreakpointList.GetCaretIndex(); int index = m_BreakpointList.GetCaretIndex();
uint32_t address = m_BreakpointList.GetItemData(index); uint32_t address = m_BreakpointList.GetItemData(index);
int len = m_BreakpointList.GetTextLen(index); int len = m_BreakpointList.GetTextLen(index);
wchar_t* rowText = (wchar_t*)malloc((len + 1) * sizeof(wchar_t)); std::wstring rowText;
rowText[len] = '\0'; rowText.resize(len);
m_BreakpointList.GetText(index, rowText);
if (*rowText == L'E') m_BreakpointList.GetText(index, (wchar_t *)rowText.data());
if (rowText[0] == L'E')
{ {
ShowAddress(address, true); ShowAddress(address, true);
} }
@ -1452,7 +1453,6 @@ LRESULT CDebugCommandsView::OnListBoxClicked(WORD /*wNotifyCode*/, WORD wID, HWN
{ {
m_Debugger->Debug_ShowMemoryLocation(address, true); m_Debugger->Debug_ShowMemoryLocation(address, true);
} }
free(rowText);
} }
return FALSE; return FALSE;
} }

View File

@ -180,26 +180,24 @@ bool CDumpMemory::DumpMemory(LPCTSTR FileName, DumpFormat Format, DWORD StartPC,
} }
uint32_t dumpLen = EndPC - StartPC; uint32_t dumpLen = EndPC - StartPC;
uint8_t* dumpBuf = (uint8_t*)malloc(dumpLen); std::unique_ptr<uint8_t> dumpBuf = std::make_unique<uint8_t>(dumpLen);
uint32_t dumpIdx = 0; uint32_t dumpIdx = 0;
for (uint32_t pc = StartPC; pc < EndPC; pc++, dumpIdx++) 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) if (!bReadable)
{ {
g_Notify->DisplayError(stdstr_f("Address error\n%s", strFile.c_str()).c_str()); g_Notify->DisplayError(stdstr_f("Address error\n%s", strFile.c_str()).c_str());
dumpFile.Close(); dumpFile.Close();
free(dumpBuf);
return false; return false;
} }
} }
dumpFile.SeekToBegin(); dumpFile.SeekToBegin();
dumpFile.Write(dumpBuf, dumpLen); dumpFile.Write(dumpBuf.get(), dumpLen);
dumpFile.Close(); dumpFile.Close();
free(dumpBuf);
return true; return true;
} }

View File

@ -972,21 +972,18 @@ LRESULT CDebugMemoryView::OnHxPaste(LPNMHDR lpNMHDR)
if (nmp->column == HX_COL_HEXDATA) if (nmp->column == HX_COL_HEXDATA)
{ {
char* data = nullptr;
// TODO: move this function to some utility class // TODO: move this function to some utility class
int length = CMemoryScanner::ParseHexString(nullptr, text); int length = CMemoryScanner::ParseHexString(nullptr, text);
if (length != 0) if (length != 0)
{ {
data = (char*)malloc(length); std::unique_ptr<char> data = std::make_unique<char>(length);
CMemoryScanner::ParseHexString(data, text); CMemoryScanner::ParseHexString(data.get(), text);
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
SetByte(nmp->address + i, data[i]); SetByte(nmp->address + i, data.get()[i]);
} }
free(data);
} }
retDataLength = length; retDataLength = length;

View File

@ -152,33 +152,17 @@ CScanResult::~CScanResult(void)
void CScanResult::SetDescription(const char* str) void CScanResult::SetDescription(const char* str)
{ {
if (m_Description != nullptr) m_Description = str;
{
free(m_Description);
}
size_t len = strlen(str);
m_Description = (char*)malloc(len + 1);
strcpy(m_Description, str);
m_Description[len] = '\0';
} }
void CScanResult::DeleteDescription(void) void CScanResult::DeleteDescription(void)
{ {
if (m_Description != nullptr) m_Description.clear();
{
free(m_Description);
m_Description = nullptr;
}
} }
const char* CScanResult::GetDescription(void) const char* CScanResult::GetDescription(void)
{ {
if (m_Description == nullptr) return m_Description.c_str();
{
return "";
}
return m_Description;
} }
int CScanResult::GetValueString(char *buffer, size_t size) int CScanResult::GetValueString(char *buffer, size_t size)

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <stdafx.h> #include <stdafx.h>
#include <string>
enum ValueType enum ValueType
{ {
@ -137,7 +138,7 @@ public:
AddressType m_AddressType; AddressType m_AddressType;
DisplayFormat m_DisplayFormat; DisplayFormat m_DisplayFormat;
bool m_bSelected; bool m_bSelected;
char* m_Description; std::string m_Description;
public: public:
int GetValueString(char* buffer, size_t size); int GetValueString(char* buffer, size_t size);

View File

@ -38,7 +38,7 @@ private:
vector<HOOKENTRY> m_Hooks; vector<HOOKENTRY> m_Hooks;
vector<INSTANCE_ENTRY> m_RunningInstances; vector<INSTANCE_ENTRY> m_RunningInstances;
vector<char*> m_LogData; vector<std::string> m_LogData;
CScriptHook* m_HookCPUExec; CScriptHook* m_HookCPUExec;
CScriptHook* m_HookCPURead; CScriptHook* m_HookCPURead;
@ -69,16 +69,9 @@ public:
return m_ScreenDC; return m_ScreenDC;
} }
inline vector<char*>* LogData()
{
return &m_LogData;
}
inline void LogText(const char* text) inline void LogText(const char* text)
{ {
char* newStr = (char*)malloc(strlen(text)); m_LogData.push_back(text);
strcpy(newStr, text);
m_LogData.push_back(newStr);
m_Debugger->Debug_RefreshScriptsWindow(); m_Debugger->Debug_RefreshScriptsWindow();
} }