From 807c1fc39724b270a03caf1191dd0abe391ad3b0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 3 May 2009 03:16:15 +0000 Subject: [PATCH] added validation check to disk_buffer_pool (and disk_io_thread in 0.14). This should catch any reference to the disk_io_thread after it has been destructed --- include/libtorrent/disk_io_thread.hpp | 6 ++++++ src/disk_io_thread.cpp | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 238d5364e..3faebdac0 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -166,6 +166,9 @@ namespace libtorrent struct disk_buffer_pool : boost::noncopyable { disk_buffer_pool(int block_size); +#ifdef TORRENT_DEBUG + ~disk_buffer_pool(); +#endif #ifdef TORRENT_DEBUG bool is_disk_buffer(char* buffer) const; @@ -217,6 +220,9 @@ namespace libtorrent #endif #ifdef TORRENT_DISK_STATS std::ofstream m_log; +#endif +#ifdef TORRENT_DEBUG + int m_magic; #endif }; diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index b399458c1..96c54827a 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -61,12 +61,24 @@ namespace libtorrent #endif #ifdef TORRENT_DISK_STATS m_log.open("disk_buffers.log", std::ios::trunc); +#endif +#ifdef TORRENT_DEBUG + m_magic = 0x1337; #endif } +#ifdef TORRENT_DEBUG + disk_buffer_pool::~disk_buffer_pool() + { + TORRENT_ASSERT(m_magic == 0x1337); + m_magic = 0; + } +#endif + #ifdef TORRENT_DEBUG bool disk_buffer_pool::is_disk_buffer(char* buffer) const { + TORRENT_ASSERT(m_magic == 0x1337); #ifdef TORRENT_DISABLE_POOL_ALLOCATOR return true; #else @@ -79,6 +91,7 @@ namespace libtorrent char* disk_buffer_pool::allocate_buffer(char const* category) { mutex_t::scoped_lock l(m_pool_mutex); + TORRENT_ASSERT(m_magic == 0x1337); #ifdef TORRENT_DISABLE_POOL_ALLOCATOR char* ret = page_aligned_allocator::malloc(m_block_size); #else @@ -109,6 +122,7 @@ namespace libtorrent { TORRENT_ASSERT(buf); mutex_t::scoped_lock l(m_pool_mutex); + TORRENT_ASSERT(m_magic == 0x1337); #ifdef TORRENT_STATS --m_allocations; TORRENT_ASSERT(m_categories.find(m_buf_to_category[buf]) @@ -139,6 +153,7 @@ namespace libtorrent char* disk_buffer_pool::allocate_buffers(int num_blocks, char const* category) { mutex_t::scoped_lock l(m_pool_mutex); + TORRENT_ASSERT(m_magic == 0x1337); #ifdef TORRENT_DISABLE_POOL_ALLOCATOR char* ret = page_aligned_allocator::malloc(m_block_size * num_blocks); #else @@ -169,6 +184,7 @@ namespace libtorrent TORRENT_ASSERT(buf); TORRENT_ASSERT(num_blocks >= 1); mutex_t::scoped_lock l(m_pool_mutex); + TORRENT_ASSERT(m_magic == 0x1337); #ifdef TORRENT_STATS m_allocations -= num_blocks; TORRENT_ASSERT(m_categories.find(m_buf_to_category[buf]) @@ -198,6 +214,7 @@ namespace libtorrent void disk_buffer_pool::release_memory() { + TORRENT_ASSERT(m_magic == 0x1337); #ifndef TORRENT_DISABLE_POOL_ALLOCATOR mutex_t::scoped_lock l(m_pool_mutex); m_pool.release_memory();