• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            把握命運,追逐夢想

            對自己所做的事要有興趣,同時還要能夠堅持不懈

            統計

            留言簿(1)

            閱讀排行榜

            評論排行榜

            vc上的內存泄露調試的例子

            /*****************************************************************
             *             ?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 );
            }
            大概解釋一下,這里的函數都是什么作用:
            1、首先是剛開始的六個函數,這六個函數的作用就是讓接下來的內存調試函數輸出調試信息到控制臺。后面的函數都是實際調試內存的,如果之前調用了這六個函數,那么調試信息會輸出到控制臺。
            2、_CrtMemDumpAllObjectsSince( NULL )函數的作用是輸出目前為止,在堆中申請的空間的所有信息,包括地址,大小,內容。
            3、_CrtDumpMemoryLeaks( )函數的作用是檢測到目前為止,還有哪些堆中申請的內存沒有釋放,包括地址,大小等
            4、SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF )宏的作用是在程序結束的時候檢測堆內存是否還有泄露,作用同CrtDumpMemoryLeaks( )一樣,只不過是在所有該釋放的都結束之后,進行最后的檢查。
            (未完,以后補充)

            posted on 2009-08-11 19:49 把握命運 閱讀(797) 評論(0)  編輯 收藏 引用

            日本欧美国产精品第一页久久| 久久中文字幕一区二区| 中文字幕精品久久久久人妻| 亚洲精品第一综合99久久| 99久久精品费精品国产一区二区| 曰曰摸天天摸人人看久久久| 久久婷婷午色综合夜啪| 国产一区二区三区久久精品| 久久性生大片免费观看性| 亚洲中文字幕无码久久2020| 精品久久久久久99人妻| 蜜臀久久99精品久久久久久小说| 99久久精品免费看国产一区二区三区| 国产成人综合久久精品红| 色综合久久中文色婷婷| 亚洲AV无一区二区三区久久| 久久久久久无码国产精品中文字幕| 久久久久高潮毛片免费全部播放| 日日狠狠久久偷偷色综合免费| 久久福利青草精品资源站| 伊人久久大香线蕉综合影院首页| 久久久久噜噜噜亚洲熟女综合| 久久亚洲AV永久无码精品| 久久久久亚洲Av无码专| 久久99热这里只有精品66| 精品一区二区久久| 精品国产乱码久久久久久郑州公司| 久久99国产精品久久99小说| 久久精品无码专区免费| 一本大道久久a久久精品综合| 精品国产一区二区三区久久| 久久综合给久久狠狠97色| 无码AV波多野结衣久久| 波多野结衣AV无码久久一区| 久久精品国产久精国产一老狼| 久久精品国产免费观看三人同眠| 国内精品久久久久影院亚洲| 欧美亚洲国产精品久久| 久久久久无码精品国产不卡| 久久综合香蕉国产蜜臀AV| 99久久免费国产特黄|