valgrind and throw exception

在C++的代码段中,使用了throw来抛出异常,这是一种非常不好的方法。

一般我们使用C++代码,或多或少都使用到了STL,而抛出异常的时候,STL里面的容器来不及调用析构函数,导致valgrind会报blocks are possibly lost的警告信息,这使本身代码存在的一些问题被长长的possibly lost给忽略掉,因此建议在C/C++代码中少抛出异常,而使用返回值。
对于STL,也有一种方法强制内存的释放,详情可见valgrind官方的FAQ文档

  • With GCC 2.91, 2.95, 3.0 and 3.1, compile all source using the STL with -D__USE_MALLOC. Beware! This was removed from GCC starting with version 3.3.

  • With GCC 3.2.2 and later, you should export the environment variable GLIBCPP_FORCE_NEW before running your program.

  • With GCC 3.4 and later, that variable has changed name to GLIBCXX_FORCE_NEW.

那么去除valgrind里面的警告,即需要设置环境变量:
export GLIBCXX_FORCE_NEW=1
且需要重新编译一下程序。

Monthly Archives

Pages

Powered by Movable Type 7.7.2

About this Entry

This page contains a single entry by Cnangel published on July 25, 2012 11:15 AM.

log4cpp 1.1.0 patch was the previous entry in this blog.

Halide - 图像处理的语言 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.