• <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>

            huyutian

            他強由他強,清風拂山崗;他橫由他橫,明月照大江。他自狠來他自惡,我自一口真氣足

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              20 隨筆 :: 47 文章 :: 22 評論 :: 0 Trackbacks
            最近寫一個程序時,出現(xiàn)了內(nèi)存泄漏問題。然后到網(wǎng)上搜索了一下,發(fā)現(xiàn)了一個簡單易用的開源內(nèi)存泄漏檢測工具Visual Leak Detector (VLD) ,在visual c++上工作的很好,其他環(huán)境尚未研究。源代碼、安裝文件可以從codeproject下載,目前最新版本是v1.9b。
            先說說這個工具的使用。安裝完畢,在需要調(diào)試的項目中加入把Visual Leak Detector 的Include目錄和LIB目錄添加到項目的路徑中去。再在需要監(jiān)視的cpp文件頭中加入#include <vld.h>,如果是MFC程序,就可以直接在stdafx.h的最后面加上這一句。剩下的就是運行調(diào)試程序,在output窗口察看結(jié)果了。
            我的錯誤信息如下。
            ---------- Block 251168 at 0x00FE9998: 16 bytes ----------
              Call Stack:
                c:\program files\microsoft visual studio .net 2003\vc7\include\xmemory (34): std::_Allocate<char>
                c:\program files\microsoft visual studio .net 2003\vc7\include\xmemory (137): std::allocator<char>::allocate
                c:\program files\microsoft visual studio .net 2003\vc7\include\xstring (1454): std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Copy
                c:\program files\microsoft visual studio .net 2003\vc7\include\xstring (1485): std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Grow
                c:\program files\microsoft visual studio .net 2003\vc7\include\xstring (612): std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign
                c:\program files\microsoft visual studio .net 2003\vc7\include\xstring (623): std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign
                c:\program files\microsoft visual studio .net 2003\vc7\include\xstring (473): std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=
                d:\projects\client\quoteclient\tradetransaction.cpp (2143): CTradeTransaction::AnsReadTodayOrder
                d:\projects\client\quoteclient\tradetransaction.cpp (2694): CTradeTransaction::ProcessAllReq
                d:\projects\client\quoteclient\tradesocket.cpp (93): CTradeSocket::ReceivePacket
                d:\projects\client\quoteclient\tradethread.cpp (146): CTradeThread::RunWorkerInternal
                d:\projects\client\quoteclient\tradethread.cpp (97): CTradeThread::WorkerThreadProc
                f:\vs70builds\3077\vc\crtbld\crt\src\threadex.c (241): _threadstartex
                0x7C80B729 (File and line number not available): GetModuleFileNameA
              Data:
                CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
            點擊內(nèi)存泄漏條目可以定位到某一行源代碼,可以看出主要是使用std::string不當造成的內(nèi)存泄漏。
            進一步分析發(fā)現(xiàn)是由于對某個結(jié)構(gòu)體的成員變量std::string誤使用了memset初始化造成的。
            struct tag_myStruct
            {
            std::string id;
            int value;
            }
            總結(jié)一下,在c語言中,結(jié)構(gòu)體的初始化,是可以直接memset(pStruct, 0,  sizeof(tag_myStruct));
            但在c++中必須小心,比如上面的結(jié)構(gòu)實際上就是一個類了,習慣性的用memset就會出錯。為防止這種慣性思維,建議將所有復雜結(jié)構(gòu)直接用class來申明.然后定義缺省構(gòu)造函數(shù)。否則將來使用時一不小心,就可能造成不測。

            補充:盡量不要在C++程序中使用C風格的代碼。但這個是需要一些時間來調(diào)整的。
            posted on 2010-01-26 14:57 胡雨田 閱讀(1111) 評論(0)  編輯 收藏 引用 所屬分類: 編程技巧
            婷婷久久五月天| 久久国产精品无码HDAV| 欧美精品九九99久久在观看| 中文字幕久久精品| 久久99免费视频| 成人久久免费网站| 久久久久无码中| 久久中文字幕一区二区| 漂亮人妻被中出中文字幕久久| 国产精品久久久久影院色| 一本久久精品一区二区| 品成人欧美大片久久国产欧美...| 久久人与动人物a级毛片| 久久久网中文字幕| 91久久精品视频| 久久91精品久久91综合| 亚洲精品无码成人片久久| 亚洲欧美成人久久综合中文网| 亚洲天堂久久精品| 美女写真久久影院| 国产精品99久久久久久人| 久久久免费精品re6| 久久久久久久波多野结衣高潮| 久久综合九色综合欧美就去吻| 久久精品无码一区二区app| 久久精品嫩草影院| 久久国产精品国产自线拍免费| AV狠狠色丁香婷婷综合久久| 久久天天躁狠狠躁夜夜2020一| 亚洲精品成人久久久| 亚洲欧洲久久av| 亚洲精品乱码久久久久66| 综合久久国产九一剧情麻豆| 久久免费看黄a级毛片| 欧美亚洲色综久久精品国产| 久久99精品国产麻豆宅宅| 亚洲AV无码久久精品狠狠爱浪潮| 婷婷伊人久久大香线蕉AV | 久久久久亚洲精品男人的天堂| 91精品婷婷国产综合久久| 久久伊人五月天论坛|