diff --git a/pykd/dbgdump.cpp b/pykd/dbgdump.cpp index df27657..a57c9a2 100644 --- a/pykd/dbgdump.cpp +++ b/pykd/dbgdump.cpp @@ -1,5 +1,7 @@ #include "stdafx.h" +#include + #include "dbgext.h" #include "dbgdump.h" #include "dbgexcept.h" @@ -13,21 +15,19 @@ dbgLoadDump( const std::string &fileName ) { HRESULT hres; - wchar_t *fileNameW = NULL; - try { - fileNameW = new wchar_t [ fileName.size()+ 1 ]; + std::vector fileNameW( fileName.size()+ 1 ); MultiByteToWideChar( CP_ACP, 0, fileName.c_str(), fileName.size() + 1, - fileNameW, + &fileNameW[0], fileName.size() + 1 ); - hres = dbgExt->client4->OpenDumpFileWide( fileNameW, NULL ); + hres = dbgExt->client4->OpenDumpFileWide( &fileNameW[0], NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugClient4::OpenDumpFileWide failed" ); @@ -37,9 +37,6 @@ dbgLoadDump( const std::string &fileName ) dbgSessionStarted = true; - if ( fileNameW ) - delete[] fileNameW; - return "loaded ok"; } catch( std::exception& ) @@ -54,9 +51,6 @@ dbgLoadDump( const std::string &fileName ) std::string result = "failed to open dump "; result += fileName; - if ( fileNameW ) - delete[] fileNameW; - return result; } diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp index 73ea371..8a9a5e2 100644 --- a/pykd/dbgext.cpp +++ b/pykd/dbgext.cpp @@ -323,9 +323,9 @@ py( PDEBUG_CLIENT4 client, PCSTR args) { DWORD oldCurDirLen = GetCurrentDirectoryA( 0, NULL ); - char *oldCurDirCstr = new char[ oldCurDirLen ]; + std::vector oldCurDirCstr(oldCurDirLen); - GetCurrentDirectoryA( oldCurDirLen, oldCurDirCstr ); + GetCurrentDirectoryA( oldCurDirLen, &oldCurDirCstr[0] ); SetCurrentDirectoryA( filePath.c_str() ); @@ -357,10 +357,7 @@ py( PDEBUG_CLIENT4 client, PCSTR args) Py_XDECREF(traceback); } - SetCurrentDirectoryA( oldCurDirCstr ); - - delete[] oldCurDirCstr; - + SetCurrentDirectoryA( &oldCurDirCstr[0] ); } else { diff --git a/pykd/dbgmem.cpp b/pykd/dbgmem.cpp index 47f9cb8..70e0981 100644 --- a/pykd/dbgmem.cpp +++ b/pykd/dbgmem.cpp @@ -1,5 +1,8 @@ #include "stdafx.h" +#include +#include + #include "dbgext.h" #include "dbgexcept.h" #include "dbgmem.h" @@ -68,34 +71,34 @@ compareMemory( ULONG64 addr1, ULONG64 addr2, ULONG length, BOOLEAN phyAddr ) addr1 = addr64( addr1 ); addr2 = addr64( addr2 ); - char* m1 = new char[length]; - char* m2 = new char[length]; + boost::scoped_array m1(new char[length]); + boost::scoped_array m2(new char[length]); try { if ( phyAddr == FALSE ) { - hres = dbgExt->dataSpaces->ReadVirtual( addr1, m1, length, NULL ); + hres = dbgExt->dataSpaces->ReadVirtual( addr1, m1.get(), length, NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugDataSpace::ReadVirtual failed" ); - hres = dbgExt->dataSpaces->ReadVirtual( addr2, m2, length, NULL ); + hres = dbgExt->dataSpaces->ReadVirtual( addr2, m2.get(), length, NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugDataSpace::ReadVirtual failed" ); } else { - hres = dbgExt->dataSpaces->ReadPhysical( addr1, m1, length, NULL ); + hres = dbgExt->dataSpaces->ReadPhysical( addr1, m1.get(), length, NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugDataSpace::ReadPhysical failed" ); - hres = dbgExt->dataSpaces->ReadPhysical( addr2, m2, length, NULL ); + hres = dbgExt->dataSpaces->ReadPhysical( addr2, m2.get(), length, NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugDataSpace::ReadPhysical failed" ); } - result = memcmp( m1, m2, length ) == 0; + result = memcmp( m1.get(), m2.get(), length ) == 0; } catch( std::exception &e ) @@ -107,9 +110,6 @@ compareMemory( ULONG64 addr1, ULONG64 addr2, ULONG length, BOOLEAN phyAddr ) dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); } - delete[] m1; - delete[] m2; - return result; } @@ -120,42 +120,34 @@ loadPtrArray( ULONG64 address, ULONG number ) { if ( is64bitSystem() ) { - ULONG64 *buffer = new ULONG64[ number ]; + boost::scoped_array buffer(new ULONG64[number]); - if ( loadMemory( address, buffer, number*sizeof(ULONG64) ) ) + if ( loadMemory( address, buffer.get(), number*sizeof(ULONG64) ) ) { boost::python::dict arr; for ( ULONG i = 0; i < number; ++i ) arr[i] = buffer[i]; - delete[] buffer; - return arr; } - delete[] buffer; - return boost::python::object(); } else { - ULONG *buffer = new ULONG[ number ]; + boost::scoped_array buffer(new ULONG[number]); - if ( loadMemory( address, buffer, number*sizeof(ULONG) ) ) + if ( loadMemory( address, buffer.get(), number*sizeof(ULONG) ) ) { boost::python::dict arr; for ( ULONG i = 0; i < number; ++i ) arr[i] = addr64( buffer[i] ); - delete[] buffer; - return arr; } - delete[] buffer; - return boost::python::object(); } } @@ -232,7 +224,6 @@ loadUnicodeStr( ULONG64 address ) USHORT length; USHORT maximumLength; ULONG64 buffer = 0; - wchar_t *str = NULL; do { @@ -270,22 +261,17 @@ loadUnicodeStr( ULONG64 address ) address += 4; } - wchar_t *str = new wchar_t[ length/2 ]; + std::vector str(length / 2); - if ( !loadMemory( buffer, str, length ) ) + if ( !loadMemory( buffer, &str[0], length ) ) break; - std::wstring strValue(str, length/2); + std::wstring strValue(&str[0], length/2); - delete[] str; - return boost::python::object( strValue ); } while( FALSE ); - if ( str ) - delete[] str; - return boost::python::object( "" ); } @@ -298,7 +284,6 @@ loadAnsiStr( ULONG64 address ) USHORT length; USHORT maximumLength; ULONG64 buffer = 0; - char *str = NULL; do { @@ -337,22 +322,17 @@ loadAnsiStr( ULONG64 address ) } - str = new char [ length ]; + std::vector str(length); - if ( !loadMemory( buffer, str, length ) ) + if ( !loadMemory( buffer, &str[0], length ) ) break; - std::string strVal ( str, length ); + std::string strVal ( &str[0], length ); - delete[] str; - return boost::python::object( strVal ); } while( FALSE ); - if ( str ) - delete[] str; - return boost::python::object( "" ); } @@ -436,7 +416,7 @@ loadCStr( ULONG64 address ) address = addr64( address ); - char* buffer = new char[maxLength]; + boost::scoped_array buffer(new char[maxLength]); try { @@ -444,14 +424,14 @@ loadCStr( ULONG64 address ) dbgExt->dataSpaces4->ReadMultiByteStringVirtual( address, maxLength, - buffer, + buffer.get(), maxLength, NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugDataSpace4::ReadMultiByteStringVirtual failed" ); - strObj = boost::python::object( std::string( buffer ) ); + strObj = boost::python::object( std::string( buffer.get() ) ); } catch( std::exception &e ) @@ -463,8 +443,6 @@ loadCStr( ULONG64 address ) dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); } - delete[] buffer; - return strObj; } @@ -478,7 +456,7 @@ loadWStr( ULONG64 address ) address = addr64( address ); - wchar_t* buffer = new wchar_t[maxLength]; + boost::scoped_array buffer(new wchar_t[maxLength]); try { @@ -486,14 +464,14 @@ loadWStr( ULONG64 address ) dbgExt->dataSpaces4->ReadUnicodeStringVirtualWide( address, maxLength*sizeof(wchar_t), - buffer, + buffer.get(), maxLength, NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugDataSpace4::ReadUnicodeStringVirtualWide failed" ); - strObj = boost::python::object( std::wstring(buffer) ); + strObj = boost::python::object( std::wstring(buffer.get()) ); } catch( std::exception &e ) @@ -505,8 +483,6 @@ loadWStr( ULONG64 address ) dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); } - delete[] buffer; - return strObj; } diff --git a/pykd/dbgmem.h b/pykd/dbgmem.h index 775d9e9..a9b57ba 100644 --- a/pykd/dbgmem.h +++ b/pykd/dbgmem.h @@ -2,7 +2,7 @@ #include #include - +#include ///////////////////////////////////////////////////////////////////////////////// @@ -23,23 +23,18 @@ template boost::python::object loadArray( ULONG64 address, ULONG number, BOOLEAN phyAddr = FALSE ) { - T *buffer = new T[ number ]; + boost::scoped_array buffer(new T[number]); - if ( loadMemory( address, buffer, number*sizeof(T), phyAddr ) ) + if ( loadMemory( address, buffer.get(), number*sizeof(T), phyAddr ) ) { boost::python::list lst; for ( ULONG i = 0; i < number; ++i ) lst.append( buffer[i] ); - //arr[i] = buffer[i]; - delete[] buffer; - return lst; } - delete[] buffer; - return boost::python::object(); } diff --git a/pykd/dbgmodule.cpp b/pykd/dbgmodule.cpp index 185e941..22cbe48 100644 --- a/pykd/dbgmodule.cpp +++ b/pykd/dbgmodule.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include +#include #include "dbgext.h" #include "dbgmodule.h" @@ -246,7 +247,6 @@ void dbgModuleClass::getImagePath() { HRESULT hres; - PWSTR pathBuffer = NULL; try { @@ -255,13 +255,13 @@ dbgModuleClass::getImagePath() if ( FAILED( hres ) ) throw DbgException( "IDebugSymbol3::GetImagePathWide failed" ); - pathBuffer = new WCHAR [ pathSize ]; + std::vector pathBuffer(pathSize); - hres = dbgExt->symbols3->GetSymbolPathWide( pathBuffer, pathSize, NULL ); + hres = dbgExt->symbols3->GetSymbolPathWide( &pathBuffer[0], pathSize, NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugSymbol3::GetImagePathWide failed" ); - std::wstring symPath( pathBuffer, pathSize ); + std::wstring symPath( &pathBuffer[0], pathSize ); std::wstring altName = m_debugInfo.CVData; altName = altName.substr( 0, altName.find_last_of(L".") ); @@ -314,9 +314,6 @@ dbgModuleClass::getImagePath() { dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); } - - if ( pathBuffer ) - delete[] pathBuffer; } std::string diff --git a/pykd/dbgpath.cpp b/pykd/dbgpath.cpp index 9cf549c..dc9a18e 100644 --- a/pykd/dbgpath.cpp +++ b/pykd/dbgpath.cpp @@ -1,5 +1,7 @@ #include "stdafx.h" +#include + #include "dbgpath.h" #include @@ -15,24 +17,25 @@ DbgPythonPath::DbgPythonPath() enviromentSize = GetEnvironmentVariableA( "PYTHONPATH", NULL, enviromentSize ); - char *enviromentBuffer = new char[ enviromentSize ]; - - GetEnvironmentVariableA( "PYTHONPATH", enviromentBuffer, enviromentSize ); + std::vector enviromentBuffer(enviromentSize); - typedef boost::escaped_list_separator char_separator_t; - typedef boost::tokenizer< char_separator_t > char_tokenizer_t; - - std::string pytonPath( enviromentBuffer ); - - char_tokenizer_t token( pytonPath, char_separator_t( "", "; \t", "\"" ) ); + if (!enviromentBuffer.empty()) + { + GetEnvironmentVariableA( "PYTHONPATH", &enviromentBuffer[0], enviromentSize ); - for ( char_tokenizer_t::iterator it = token.begin(); it != token.end(); ++it ) - { - if ( *it != "" ) - m_pathList.push_back( *it ); - } - - delete[] enviromentBuffer; + typedef boost::escaped_list_separator char_separator_t; + typedef boost::tokenizer< char_separator_t > char_tokenizer_t; + + std::string pytonPath( &enviromentBuffer[0] ); + + char_tokenizer_t token( pytonPath, char_separator_t( "", "; \t", "\"" ) ); + + for ( char_tokenizer_t::iterator it = token.begin(); it != token.end(); ++it ) + { + if ( *it != "" ) + m_pathList.push_back( *it ); + } + } } /////////////////////////////////////////////////////////////////////////////// @@ -84,7 +87,7 @@ DbgPythonPath::findPath( if ( bufSize > 0 ) { - char *fullFileNameCStr = new char[ bufSize ]; + std::vector fullFileNameCStr(bufSize); char *partFileNameCStr = NULL; SearchPathA( @@ -92,13 +95,11 @@ DbgPythonPath::findPath( fileName.c_str(), pyExt ? NULL : ".py", bufSize, - fullFileNameCStr, + &fullFileNameCStr[0], &partFileNameCStr ); - fullFileName = std::string( fullFileNameCStr ); - filePath = std::string( fullFileNameCStr, partFileNameCStr ); - - delete[] fullFileNameCStr; + fullFileName = std::string( &fullFileNameCStr[0] ); + filePath = std::string( &fullFileNameCStr[0], partFileNameCStr ); return true; } @@ -120,7 +121,7 @@ DbgPythonPath::findPath( if ( bufSize > 0 ) { - char *fullFileNameCStr = new char[ bufSize ]; + std::vector fullFileNameCStr(bufSize); char *partFileNameCStr = NULL; SearchPathA( @@ -128,13 +129,11 @@ DbgPythonPath::findPath( fileName.c_str(), pyExt ? NULL : ".py", bufSize, - fullFileNameCStr, + &fullFileNameCStr[0], &partFileNameCStr ); - fullFileName = std::string( fullFileNameCStr ); - filePath = std::string( fullFileNameCStr, partFileNameCStr ); - - delete[] fullFileNameCStr; + fullFileName = std::string( &fullFileNameCStr[0] ); + filePath = std::string( &fullFileNameCStr[0], partFileNameCStr ); return true; } diff --git a/pykd/dbgprocess.cpp b/pykd/dbgprocess.cpp index 46de8be..6f0ba5c 100644 --- a/pykd/dbgprocess.cpp +++ b/pykd/dbgprocess.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include +#include #include "dbgprocess.h" #include "dbgext.h" @@ -13,7 +14,6 @@ boost::python::object getThreadList() { HRESULT hres; - PULONG ids = NULL; ULONG i; ULONG oldThreadId = 0; @@ -24,8 +24,8 @@ getThreadList() if ( FAILED( hres ) ) throw DbgException( "IDebugSystemObjects::GetNumberThreads failed" ); - ids = new ULONG[threadCount]; - hres = dbgExt->system->GetThreadIdsByIndex( 0, threadCount, ids, NULL ); + boost::scoped_array ids(new ULONG[threadCount]); + hres = dbgExt->system->GetThreadIdsByIndex( 0, threadCount, ids.get(), NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugSystemObjects::GetThreadIdsByIndex failed" ); @@ -46,9 +46,6 @@ getThreadList() threadList.append( threadOffset ); } - if ( ids ) - delete[] ids; - return threadList; } catch( std::exception &e ) @@ -63,9 +60,6 @@ getThreadList() if ( oldThreadId ) dbgExt->system->SetCurrentThreadId( oldThreadId ); - if ( ids ) - delete[] ids; - return boost::python::list(); } @@ -135,7 +129,6 @@ boost::python::object getCurrentStack() { HRESULT hres; - PDEBUG_STACK_FRAME frames = NULL; ULONG currentScope = 0; try { @@ -144,10 +137,10 @@ getCurrentStack() if ( FAILED( hres ) ) throw DbgException( "IDebugSymbol3::GetCurrentScopeFrameIndex failed" ); - frames = new DEBUG_STACK_FRAME [ 1000 ]; + boost::scoped_array frames(new DEBUG_STACK_FRAME [ 1000 ]); ULONG filledFrames; - hres = dbgExt->control->GetStackTrace( 0, 0, 0, frames, 1000, &filledFrames ); + hres = dbgExt->control->GetStackTrace( 0, 0, 0, frames.get(), 1000, &filledFrames ); if ( FAILED( hres ) ) throw DbgException( "IDebugControl::GetStackTrace failed" ); @@ -166,9 +159,6 @@ getCurrentStack() frameList.append( frameObj ); } - if ( frames ) - delete[] frames; - dbgExt->symbols3->SetScopeFrameByIndex( currentScope ); return frameList; @@ -183,9 +173,6 @@ getCurrentStack() dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); } - if ( frames ) - delete[] frames; - dbgExt->symbols3->SetScopeFrameByIndex( currentScope ); return boost::python::object(); diff --git a/pykd/dbgsystem.cpp b/pykd/dbgsystem.cpp index 21d5267..bd2c55d 100644 --- a/pykd/dbgsystem.cpp +++ b/pykd/dbgsystem.cpp @@ -1,5 +1,7 @@ #include "stdafx.h" +#include + #include #include "dbgext.h" #include "dbgexcept.h" @@ -46,7 +48,6 @@ std::string dbgSymPath() { HRESULT hres; - char *path = NULL; std::string pathStr; try { @@ -54,12 +55,12 @@ dbgSymPath() ULONG size; dbgExt->symbols->GetSymbolPath( NULL, 0, &size ); - path = new char[ size ]; - hres = dbgExt->symbols->GetSymbolPath( path, size, NULL ); + std::vector path(size); + hres = dbgExt->symbols->GetSymbolPath( &path[0], size, NULL ); if ( FAILED( hres ) ) throw DbgException( "IDebugSymbols::GetSymbolPath failed" ); - pathStr = path; + pathStr = &path[0]; } catch( std::exception &e ) @@ -71,9 +72,6 @@ dbgSymPath() dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); } - if ( path ) - delete[] path; - return pathStr; }