返回值的不正确处理
有时,某些函数会返回对动态分配的内存的引用。跟踪该内存位置并正确地处理它就成为了 calling
函数的职责。
char *func ( ) { return malloc(20); // make sure to memset this location to ‘\0’… } void callingFunc ( ) { func ( ); // Problem lies here }
在上面的示例中,callingFunc()
函数中对 func()
函数的调用未处理该内存位置的返回地址。结果,func()
函数所分配的 20 个字节的块就丢失了,并导致了内存泄漏
参考:http://www.ibm.com/developerworks/cn/aix/library/au-toughgame/