From f8620fcd0b4dcc6006a6a228cd5cc79cc82a83fc Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 22 Mar 2011 07:27:23 +0000 Subject: Fixed some memory leaks. Only one was mine ;P git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7392 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/ChunkFile.h | 34 +++++++++-------------------- Source/Core/Common/Src/ConsoleListener.cpp | 35 +++++++++++++++--------------- Source/Core/Common/Src/FileUtil.cpp | 2 +- 3 files changed, 29 insertions(+), 42 deletions(-) (limited to 'Source/Core/Common') diff --git a/Source/Core/Common/Src/ChunkFile.h b/Source/Core/Common/Src/ChunkFile.h index 5f897b1359..9dd1578890 100644 --- a/Source/Core/Common/Src/ChunkFile.h +++ b/Source/Core/Common/Src/ChunkFile.h @@ -119,9 +119,12 @@ public: // Store vectors. template - void Do(std::vector &x) { - // TODO - PanicAlert("Do(vector<>) does not yet work."); + void Do(std::vector &x) + { + u32 vec_size = (u32)x.size(); + Do(vec_size); + x.resize(vec_size); + DoArray(&x[0], vec_size); } // Store strings. @@ -138,23 +141,6 @@ public: } (*ptr) += stringLen; } - - void DoBuffer(u8** pBuffer, u32& _Size) - { - Do(_Size); - - if (_Size > 0) { - switch (mode) { - case MODE_READ: delete[] *pBuffer; *pBuffer = new u8[_Size]; memcpy(*pBuffer, *ptr, _Size); break; - case MODE_WRITE: memcpy(*ptr, *pBuffer, _Size); break; - case MODE_MEASURE: break; - case MODE_VERIFY: if(*pBuffer) for(u32 i = 0; i < _Size; i++) _dbg_assert_msg_(COMMON, (*pBuffer)[i] == (*ptr)[i], "Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n", (*pBuffer)[i], (*pBuffer)[i], &(*pBuffer)[i], (*ptr)[i], (*ptr)[i], &(*ptr)[i]); break; - } - } else { - *pBuffer = NULL; - } - (*ptr) += _Size; - } template void DoArray(T *x, int count) { @@ -260,9 +246,9 @@ public: u8 *ptr = 0; PointerWrap p(&ptr, PointerWrap::MODE_MEASURE); _class.DoState(p); - size_t sz = (size_t)ptr; - u8 *buffer = new u8[sz]; - ptr = buffer; + size_t const sz = (size_t)ptr; + std::vector buffer(sz); + ptr = &buffer[0]; p.SetMode(PointerWrap::MODE_WRITE); _class.DoState(p); @@ -279,7 +265,7 @@ public: return false; } - if (!pFile.WriteBytes(buffer, sz)) + if (!pFile.WriteBytes(&buffer[0], sz)) { ERROR_LOG(COMMON,"ChunkReader: Failed writing data"); return false; diff --git a/Source/Core/Common/Src/ConsoleListener.cpp b/Source/Core/Common/Src/ConsoleListener.cpp index 4240061933..e0b2bf4d36 100644 --- a/Source/Core/Common/Src/ConsoleListener.cpp +++ b/Source/Core/Common/Src/ConsoleListener.cpp @@ -22,6 +22,7 @@ #include #ifdef _WIN32 #include +#include #else #include #endif @@ -175,8 +176,8 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool bool DAft = true; std::string SLog = ""; - HWND hWnd = GetConsoleWindow(); - HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + const HWND hWnd = GetConsoleWindow(); + const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Get console info CONSOLE_SCREEN_BUFFER_INFO ConInfo; @@ -189,30 +190,30 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool DWORD cCharsRead = 0; COORD coordScreen = { 0, 0 }; - std::vector Str; - std::vector Attr; + static const int MAX_BYTES = 1024 * 16; + + std::vector> Str; + std::vector> Attr; + // ReadConsoleOutputAttribute seems to have a limit at this level - const int MAX_BYTES = 1024 * 16; - int ReadBufferSize = MAX_BYTES - 32; + static const int ReadBufferSize = MAX_BYTES - 32; + DWORD cAttrRead = ReadBufferSize; DWORD BytesRead = 0; - int i = 0; - int LastAttrRead = 0; while (BytesRead < BufferSize) { - Str.push_back(new char[MAX_BYTES]); - if (!ReadConsoleOutputCharacter(hConsole, Str[i], ReadBufferSize, coordScreen, &cCharsRead)) + Str.resize(Str.size() + 1); + if (!ReadConsoleOutputCharacter(hConsole, Str.back().data(), ReadBufferSize, coordScreen, &cCharsRead)) SLog += StringFromFormat("WriteConsoleOutputCharacter error"); - Attr.push_back(new WORD[MAX_BYTES]); - if (!ReadConsoleOutputAttribute(hConsole, Attr[i], ReadBufferSize, coordScreen, &cAttrRead)) + + Attr.resize(Attr.size() + 1); + if (!ReadConsoleOutputAttribute(hConsole, Attr.back().data(), ReadBufferSize, coordScreen, &cAttrRead)) SLog += StringFromFormat("WriteConsoleOutputAttribute error"); // Break on error if (cAttrRead == 0) break; BytesRead += cAttrRead; - i++; coordScreen = GetCoordinates(BytesRead, ConInfo.dwSize.X); - LastAttrRead = cAttrRead; } // Letter space int LWidth = (int)(floor((float)Width / 8.0f) - 1.0f); @@ -232,16 +233,16 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool DWORD cAttrWritten = 0; for (size_t i = 0; i < Attr.size(); i++) { - if (!WriteConsoleOutputCharacter(hConsole, Str[i], ReadBufferSize, coordScreen, &cCharsWritten)) + if (!WriteConsoleOutputCharacter(hConsole, Str[i].data(), ReadBufferSize, coordScreen, &cCharsWritten)) SLog += StringFromFormat("WriteConsoleOutputCharacter error"); - if (!WriteConsoleOutputAttribute(hConsole, Attr[i], ReadBufferSize, coordScreen, &cAttrWritten)) + if (!WriteConsoleOutputAttribute(hConsole, Attr[i].data(), ReadBufferSize, coordScreen, &cAttrWritten)) SLog += StringFromFormat("WriteConsoleOutputAttribute error"); BytesWritten += cAttrWritten; coordScreen = GetCoordinates(BytesWritten, LBufWidth); } - int OldCursor = ConInfo.dwCursorPosition.Y * ConInfo.dwSize.X + ConInfo.dwCursorPosition.X; + const int OldCursor = ConInfo.dwCursorPosition.Y * ConInfo.dwSize.X + ConInfo.dwCursorPosition.X; COORD Coo = GetCoordinates(OldCursor, LBufWidth); SetConsoleCursorPosition(hConsole, Coo); diff --git a/Source/Core/Common/Src/FileUtil.cpp b/Source/Core/Common/Src/FileUtil.cpp index 969ba9f948..bbfc03f2fa 100644 --- a/Source/Core/Common/Src/FileUtil.cpp +++ b/Source/Core/Common/Src/FileUtil.cpp @@ -677,7 +677,7 @@ bool WriteStringToFile(bool text_file, const std::string &str, const char *filen if (!f) return false; size_t len = str.size(); - if (len != fwrite(str.data(), 1, str.size(), f)) + if (len != fwrite(str.data(), 1, str.size(), f)) // TODO: string::data() may not be contiguous { fclose(f); return false; -- cgit v1.2.3