0
0
mirror of https://github.com/yse/easy_profiler.git synced 2025-01-14 00:27:55 +08:00

(Core) Update #29 : fixed potential memory leak for NonscopedBlock + linux build

This commit is contained in:
Victor Zarubkin 2017-04-17 23:31:07 +03:00
parent c2b3a8f5dc
commit dff1d8b2a4
2 changed files with 31 additions and 4 deletions

View File

@ -260,7 +260,7 @@ extern "C" {
#elif defined(USE_STD_CHRONO)
return _ticks;
#else
return _ticks / CPU_FREQUENCY.load(std::memory_order_acquire)
return _ticks / CPU_FREQUENCY.load(std::memory_order_acquire);
#endif
}
@ -271,7 +271,7 @@ extern "C" {
#elif defined(USE_STD_CHRONO)
return _ticks / 1000;
#else
return _ticks * 1000 / CPU_FREQUENCY.load(std::memory_order_acquire)
return _ticks * 1000 / CPU_FREQUENCY.load(std::memory_order_acquire);
#endif
}
@ -610,6 +610,11 @@ void NonscopedBlock::copyname()
}
}
void NonscopedBlock::destroy()
{
m_runtimeName.std::string::~string(); // free memory used by m_runtimeName
}
//////////////////////////////////////////////////////////////////////////
ThreadStorage::ThreadStorage() : nonscopedBlocks(16), id(getCurrentThreadId()), allowChildren(true), named(false), guarded(false)

View File

@ -306,10 +306,23 @@ public:
*/
void copyname();
void destroy();
}; // END of class NonscopedBlock.
//////////////////////////////////////////////////////////////////////////
template <class T>
inline void destroy_elem(T*)
{
}
inline void destroy_elem(NonscopedBlock* _elem)
{
_elem->destroy();
}
template <class T>
class StackBuffer
{
@ -329,7 +342,13 @@ public:
~StackBuffer()
{
for (uint32_t i = 0; i < m_size; ++i)
destroy_elem(m_buffer + i);
free(m_buffer);
for (auto& elem : m_overflow)
destroy_elem(reinterpret_cast<T*>(elem.data + 0));
}
template <class ... TArgs>
@ -351,9 +370,11 @@ public:
if (m_overflow.empty())
{
// m_size should not be equal to 0 here because ProfileManager behavior does not allow such situation
if (--m_size == 0 && m_maxcapacity > m_capacity)
destroy_elem(m_buffer + --m_size);
if (m_size == 0 && m_maxcapacity > m_capacity)
{
// When stack gone empty we can resize buffer to use enough space in future
// When stack gone empty we can resize buffer to use enough space in the future
free(m_buffer);
m_maxcapacity = m_capacity = std::max(m_maxcapacity, m_capacity << 1);
m_buffer = (T*)malloc(m_capacity * sizeof(T));
@ -362,6 +383,7 @@ public:
return;
}
destroy_elem(reinterpret_cast<T*>(m_overflow.back().data + 0));
m_overflow.pop_back();
}