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();
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;
}

View File

@ -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<uint8_t> dumpBuf = std::make_unique<uint8_t>(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;
}

View File

@ -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<char> data = std::make_unique<char>(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;

View File

@ -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)

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdafx.h>
#include <string>
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);

View File

@ -38,7 +38,7 @@ private:
vector<HOOKENTRY> m_Hooks;
vector<INSTANCE_ENTRY> m_RunningInstances;
vector<char*> m_LogData;
vector<std::string> m_LogData;
CScriptHook* m_HookCPUExec;
CScriptHook* m_HookCPURead;
@ -69,16 +69,9 @@ public:
return m_ScreenDC;
}
inline vector<char*>* 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();
}