[~] refactoring: RAII idiom is used instead of manual memory management

git-svn-id: https://pykd.svn.codeplex.com/svn@61917 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\evtanazia_cp 2011-02-24 10:46:00 +00:00
parent a4993db623
commit 004a4067ea
8 changed files with 78 additions and 135 deletions

View File

@ -1,5 +1,7 @@
#include "stdafx.h"
#include <vector>
#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<wchar_t> 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;
}

View File

@ -323,9 +323,9 @@ py( PDEBUG_CLIENT4 client, PCSTR args)
{
DWORD oldCurDirLen = GetCurrentDirectoryA( 0, NULL );
char *oldCurDirCstr = new char[ oldCurDirLen ];
std::vector<char> 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
{

View File

@ -1,5 +1,8 @@
#include "stdafx.h"
#include <boost/scoped_array.hpp>
#include <vector>
#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<char> m1(new char[length]);
boost::scoped_array<char> 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<ULONG64> 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<ULONG> 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<wchar_t> str(length / 2);
if ( !loadMemory( buffer, str, length ) )
if ( !loadMemory( buffer, &str[0], length ) )
break;
std::wstring strValue(str, length/2);
delete[] str;
std::wstring strValue(&str[0], length/2);
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<char> str(length);
if ( !loadMemory( buffer, str, length ) )
if ( !loadMemory( buffer, &str[0], length ) )
break;
std::string strVal ( str, length );
delete[] str;
std::string strVal ( &str[0], length );
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<char> 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<wchar_t> 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;
}

View File

@ -2,7 +2,7 @@
#include <boost/python.hpp>
#include <boost/python/object.hpp>
#include <boost/scoped_array.hpp>
/////////////////////////////////////////////////////////////////////////////////
@ -23,23 +23,18 @@ template<typename T>
boost::python::object
loadArray( ULONG64 address, ULONG number, BOOLEAN phyAddr = FALSE )
{
T *buffer = new T[ number ];
boost::scoped_array<T> 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();
}

View File

@ -1,6 +1,7 @@
#include "stdafx.h"
#include <boost/format.hpp>
#include <vector>
#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<WCHAR> 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

View File

@ -1,5 +1,7 @@
#include "stdafx.h"
#include <vector>
#include "dbgpath.h"
#include <boost/tokenizer.hpp>
@ -15,24 +17,25 @@ DbgPythonPath::DbgPythonPath()
enviromentSize = GetEnvironmentVariableA( "PYTHONPATH", NULL, enviromentSize );
char *enviromentBuffer = new char[ enviromentSize ];
std::vector<char> enviromentBuffer(enviromentSize);
GetEnvironmentVariableA( "PYTHONPATH", enviromentBuffer, enviromentSize );
if (!enviromentBuffer.empty())
{
GetEnvironmentVariableA( "PYTHONPATH", &enviromentBuffer[0], enviromentSize );
typedef boost::escaped_list_separator<char> char_separator_t;
typedef boost::tokenizer< char_separator_t > char_tokenizer_t;
typedef boost::escaped_list_separator<char> char_separator_t;
typedef boost::tokenizer< char_separator_t > char_tokenizer_t;
std::string pytonPath( enviromentBuffer );
std::string pytonPath( &enviromentBuffer[0] );
char_tokenizer_t token( pytonPath, char_separator_t( "", "; \t", "\"" ) );
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 );
}
delete[] enviromentBuffer;
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<char> 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<char> 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;
}

View File

@ -1,6 +1,7 @@
#include "stdafx.h"
#include <boost/format.hpp>
#include <boost/scoped_array.hpp>
#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<ULONG> 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<DEBUG_STACK_FRAME> 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();

View File

@ -1,5 +1,7 @@
#include "stdafx.h"
#include <vector>
#include <exception>
#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<char> 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;
}