有沒有一種方法可以在運行時識別正在從valgrind運行的可執行文件?我有一組C++單元測試,其中一個預期std::vector::reserve
拋出std::bad_alloc
。當我在valgrind下運行它時,它完全釋放,阻止我測試內存泄漏(使用valgrind)和行爲(期待異常被拋出)。如何檢測程序是否在valgrind中運行?
這裏的再現成爲一個小例子:
#include <vector>
int main()
{
size_t uint_max = static_cast<size_t>(-1);
std::vector<char> v;
v.reserve(uint_max);
}
運行Valgrind的,我得到這樣的輸出:
Warning: silly arg (-1) to __builtin_new()
new/new[] failed and should throw an exception, but Valgrind
cannot throw exceptions and so is aborting instead. Sorry.
at 0x40192BC: VALGRIND_PRINTF_BACKTRACE (valgrind.h:319)
by 0x401C823: operator new(unsigned) (vg_replace_malloc.c:164)
by 0x80487BF: std::vector<char, std::allocator<char> >::reserve(unsigned) new_allocator.h:92)
by 0x804874D: main (vg.cxx:6)
我想修改我的單元測試來簡單地跳過有問題的代碼時它是從valgrind中運行的。這可能嗎?
感謝這一點,我一直在使用*研磨多年,但幾乎沒有觸及界面。在autoconf中包含這個宏很容易,當valgrind/valgrind.h存在時,它會在valrgind下運行時自動打開我的調試printfs()和斷言:) 再次感謝!很有幫助 – 2009-01-09 07:40:14