|
From: xDraconian <sc...@gm...> - 2015-03-24 03:24:48
|
I've done some initial measurement to identify potential memory leaks in
SimGear.
Detection was limited to execution of code covered by unit tests.
Results:
math No memory leaks detected
geometry No memory leaks detected
CSSBorder No memory leaks detected
tabbed_values No memory leaks detected
streams No memory leaks detected
SimpleMarkdown-boost_test No memory leaks detected
strutils-boost_test No memory leaks detected
utf8tolatin1-boost_test No memory leaks detected
props No memory leaks detected
easing_functions No memory leaks detected
function_list-boost_test No memory leaks detected
shared_ptr-boost_test No memory leaks detected
canvas_event-boost_test No memory leaks detected
BucketBoxTest No memory leaks detected
test_bucket 19 memory leaks (1645 bytes)
bvhtest 19 memory leaks (1645 bytes)
binobj 26 memory leaks (2521 bytes)
path 19 memory leaks (1645 bytes)
SVGpreserveAspectRatio-boost_test 19 memory leaks (1645 bytes)
metar 19 memory leaks (1645 bytes)
parse_color 2 memory leaks (240 bytes)
propertyObject 498 memory leaks (54536 bytes)
expressions 49 memory leaks (4632 bytes)
http 266 memory leaks (171197 bytes)
cppbind_ghost-boost_test 44 memory leaks (26153 bytes)
cppbind_misc-boost_test 94 memory leaks (52909 bytes)
nasal_gc_test-boost_test 22 memory leaks (24344 bytes)
nasal_num-boost_test 128 memory leaks (53576 bytes)
state_machine 50 memory leaks (4665 bytes)
canvas_element-boost_test 59 memory leaks (6485 bytes)
canvas_layout-boost_test 68 memory leaks (60637 bytes)
parseBlendFunc 22 memory leaks (2512 bytes)
animations 22 memory leaks (2512 bytes)
==============================
1445 leaks (475 Kb)
*Preliminary analysis of the results:*
The same code defect will cause multiple leaks, so the number of actual
code issues will be significantly small compared to the metrics
reflected above.
*logstream.cxx*
Recommendation
Due to the shared nature of these classes, leverage SGSharedPtr to
ensure the memory is released.
Implement LogStreamPrivate::dtor() to clean up m_callbacks
This class would benefit from some refactoring
e.g. eliminate the global scope of LogStreamPrivate
classes using default copy constructors and are missing
dtor... necessary for any class using dynamic memory allocation
logstream&
sglog()
{
// Force initialization of cerr.
static std::ios_base::Init initializer;
// http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
// in the absence of portable memory barrier ops in Simgear,
// let's keep this correct & safe
static SGMutex m;
SGGuard<SGMutex> g(m);
if( !global_logstream )
global_logstream = new logstream(); *<== dynamic memory not
released*
return *global_logstream;
}
logstream::logstream()
{
global_privateLogstream = new LogStreamPrivate; *<== dynamic memory
not released*
global_privateLogstream->startLog();
}
#if !defined(SG_WINDOWS)
m_callbacks.push_back(new StderrLogCallback(m_logClass,
m_logPriority)); *<== dynamic memory not released*
m_consoleCallbacks.push_back(m_callbacks.back());
m_consoleRequested = true;
#endif
#if defined (SG_WINDOWS) && !defined(NDEBUG)
m_callbacks.push_back(new WinDebugLogCallback(m_logClass,
m_logPriority)); *<== dynamic memory not released*
m_consoleCallbacks.push_back(m_callbacks.back());
#endif
*command**s.cxx*
Recommendation
Refactor to improve the Singleton implementation
cleanup dynamic memory
Thread safety... e.g. DCLP (Double-check locking)
SGCommandMgr*
SGCommandMgr::instance()
{
return static_instance ? static_instance : new SGCommandMgr; *<==
dynamic memory not released*
}
bool SGCommandMgr::removeCommand(const std::string& name) *<== dtor
should ensure this is called in cases where the developer missed it*
{
command_map::iterator it = _commands.find(name);
if (it == _commands.end())
return false;
delete it->second;
_commands.erase(it);
return true;
}
xDraconian
|