程序出现内存泄露,如何察看是哪段代码导致?

lgdgyd2 2008-01-10 12:12:33
结束程序时候,vc的输出如下:
Detected memory leaks!
Dumping objects ->
{20616} normal block at 0x07FD28D0, 152 bytes long.
Data: < > 8F 00 00 00 B0 02 00 00 90 00 00 00 B3 02 00 00
{20615} normal block at 0x07FD2840, 80 bytes long.
Data: < > 98 00 00 00 9C 02 00 00 94 00 00 00 9F 02 00 00
{20614} normal block at 0x07FD2778, 136 bytes long.
Data: < > 92 00 00 00 94 02 00 00 95 00 00 00 90 02 00 00
{20613} normal block at 0x07FD26E8, 80 bytes long.
Data: < > 8C 00 00 00 94 02 00 00 8C 00 00 00 96 02 00 00
{20612} normal block at 0x07FD2678, 48 bytes long.
Data: < X X > 9A 02 00 00 58 02 00 00 9D 02 00 00 58 02 00 00
{20611} normal block at 0x07FD25B8, 128 bytes long.
.................
请问高人们,如何察看是哪里的代码造成的?
...全文
1207 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
xueshuangbest 2012-10-15
  • 打赏
  • 举报
回复
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
是这么写的。。
Enderzu 2008-01-13
  • 打赏
  • 举报
回复
学习
wuhuaxing 2008-01-12
  • 打赏
  • 举报
回复
X不错
lgdgyd2 2008-01-11
  • 打赏
  • 举报
回复
首先谢谢大家的帮忙,有大家的帮助,感觉真的学到了很多知识!
to:zyyoung
请问你这个办法怎么实现?我是个菜鸟,还请多多指教阿!
Torch009 2008-01-11
  • 打赏
  • 举报
回复
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

这个办法不错
llg84 2008-01-11
  • 打赏
  • 举报
回复
应该是有的,不过具体是哪儿我就不太清楚了
wawhe 2008-01-11
  • 打赏
  • 举报
回复
学习
lgdgyd2 2008-01-11
  • 打赏
  • 举报
回复
恩?这次又有了?是不是项目属性里有什么设置?我今天下午重状了一下系统,恢复到以前的项目属性设置了,跟这个有关系么?
llg84 2008-01-11
  • 打赏
  • 举报
回复
那你试试#define new DEBUG_NEW,把条件去掉,没道理不显示啊......
lgdgyd2 2008-01-11
  • 打赏
  • 举报
回复
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
这个,原来就有这行代码,可能是别人加的,我没看到!
llg84 2008-01-11
  • 打赏
  • 举报
回复
直接加的#define new DEBUG_NEW?
lgdgyd2 2008-01-11
  • 打赏
  • 举报
回复
我看了一下,这个文件原来加过这个,但是显示的时候没有文件及行号,所以我就上来问了一下,不知道是怎么一回事!
llg84 2008-01-11
  • 打赏
  • 举报
回复
效果就是在泄露的内存前加上分配该内存的代码所在的文件及行号
lgdgyd2 2008-01-11
  • 打赏
  • 举报
回复
呵呵,对不起,llg84!我主要是不知道加上之后,是一个什么样的效果(比如输出变了),所以没有试,我试验一下奥!
dabie 2008-01-11
  • 打赏
  • 举报
回复
mark
lgdgyd2 2008-01-11
  • 打赏
  • 举报
回复
我这里找到一个msdn的例子,大家一起学习一下吧!

/*****************************************************************
* ?000 Microsoft Corporation *
* CRT_DBG1 *
* This simple program illustrates the basic debugging features *
* of the C runtime libraries, and the kind of debug output *
* that these features generate. *
*****************************************************************/

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <crtdbg.h>

// Disable deprecation warnings. The unsecure version of strcpy is
// used intentionally to show off debugging features.
#pragma warning (disable : 4996)

// This routine place comments at the head of a section of debug output
void OutputHeading( const char * explanation )
{
_RPT1( _CRT_WARN, "\n\n%s:\n**************************************\
************************************\n", explanation );
}

// The following macros set and clear, respectively, given bits
// of the C runtime library debug flag, as specified by a bitmask.
#ifdef _DEBUG
#define SET_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#define CLEAR_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#else
#define SET_CRT_DEBUG_FIELD(a) ((void) 0)
#define CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
#endif


int main( )
{
char *p1, *p2;
_CrtMemState s1, s2, s3;

#ifndef _DEBUG
printf("Skipping this for non-debug mode.\n");
return 2;
#endif

// Send all reports to STDOUT
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );

// Allocate 2 memory blocks and store a string in each
p1 = malloc( 34 );
strcpy( p1, "This is the p1 string (34 bytes)." );

p2 = malloc( 34 );
strcpy( p2, "This is the p2 string (34 bytes)." );


OutputHeading(
"Use _ASSERTE to check that the two strings are identical" );
_ASSERTE( strcmp( p1, p2 ) == 0 );

OutputHeading(
"Use a _RPT macro to report the string contents as a warning" );
_RPT2( _CRT_WARN, "p1 points to '%s' and \np2 points to '%s'\n", p1, p2 );

OutputHeading(
"Use _CRTMemDumpAllObjectsSince to check the p1 and p2 allocations" );
_CrtMemDumpAllObjectsSince( NULL );

free( p2 );

OutputHeading(
"Having freed p2, dump allocation information about p1 only" );
_CrtMemDumpAllObjectsSince( NULL );

// Store a memory checkpoint in the s1 memory-state structure
_CrtMemCheckpoint( &s1 );

// Allocate another block, pointed to by p2
p2 = malloc( 38 );
strcpy( p2, "This new p2 string occupies 38 bytes.");

// Store a 2nd memory checkpoint in s2
_CrtMemCheckpoint( &s2 );

OutputHeading(
"Dump the changes that occurred between two memory checkpoints" );
if ( _CrtMemDifference( &s3, &s1, &s2 ) )
_CrtMemDumpStatistics( &s3 );

// Free p2 again and store a new memory checkpoint in s2
free( p2 );
_CrtMemCheckpoint( &s2 );

OutputHeading(
"Now the memory state at the two checkpoints is the same" );
if ( _CrtMemDifference( &s3, &s1, &s2 ) )
_CrtMemDumpStatistics( &s3 );

strcpy( p1, "This new p1 string is over 34 bytes" );
OutputHeading( "Free p1 after overwriting the end of the allocation" );
free( p1 );

// Set the debug-heap flag so that freed blocks are kept on the
// linked list, to catch any inadvertent use of freed memory
SET_CRT_DEBUG_FIELD( _CRTDBG_DELAY_FREE_MEM_DF );

p1 = malloc( 10 );
free( p1 );
strcpy( p1, "Oops" );

OutputHeading( "Perform a memory check after corrupting freed memory" );
_CrtCheckMemory( );

// Use explicit calls to _malloc_dbg to save file name and line number
// information, and also to allocate Client type blocks for tracking
p1 = _malloc_dbg( 40, _NORMAL_BLOCK, __FILE__, __LINE__ );
p2 = _malloc_dbg( 40, _CLIENT_BLOCK, __FILE__, __LINE__ );
strcpy( p1, "p1 points to a Normal allocation block" );
strcpy( p2, "p2 points to a Client allocation block" );

// You must use _free_dbg to free a Client block
OutputHeading(
"Using free( ) to free a Client block causes an assertion failure" );
free( p1 );
free( p2 );

p1 = malloc( 10 );
OutputHeading( "Examine outstanding allocations (dump memory leaks)" );
_CrtDumpMemoryLeaks( );

// Set the debug-heap flag so that memory leaks are reported when
// the process terminates. Then, exit.
OutputHeading( "Program exits without freeing a memory block" );
SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF );
}
llg84 2008-01-11
  • 打赏
  • 举报
回复
我给的是最简单的方法,你竟然没试......
lgdgyd2 2008-01-11
  • 打赏
  • 举报
回复
rabo 和 qiujian5628 的方法我试验了一下,都能检测出内存泄露的源码所在,但是我在用rabo说的方法找泄露的内存分配点时,不太会弄,呵呵,相当菜了,多谢各位大虾帮忙,我们上头条了,哈哈
webipstin 2008-01-11
  • 打赏
  • 举报
回复
学习
l1w1w1 2008-01-11
  • 打赏
  • 举报
回复
不错,学习一下
加载更多回复(12)

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧