簡體   English   中英

boost或Visual Studio 2010中的內存泄漏

[英]Memory leak in boost or Visual Studio 2010

我一直在Boost或Visual Studio 2010中發現泄漏。像這樣泄漏的程序很簡單:

#include <boost\regex.hpp>

int main()
{
    _CrtMemState state;
    _CrtMemCheckpoint(&state);

    {
        boost::regex my_filter;
        my_filter.set_expression("filter");
    }

    _CrtMemDumpAllObjectsSince(&state);
    return 0;
}

並返回

Dumping objects ->
{174} normal block at 0x00195C28, 1024 bytes long.
 Data: <    8\          > 00 00 00 00 38 5C 19 00 00 00 00 00 00 CD CD CD 
{172} normal block at 0x00195B90, 16 bytes long.
 Data: <  <         pO  > B8 D3 3C 01 01 00 00 00 01 00 00 00 70 4F 19 00 
{171} normal block at 0x00195B48, 8 bytes long.
 Data: < P      > BC 50 19 00 00 00 00 00 
{170} normal block at 0x00195AF8, 16 bytes long.
 Data: <  <         XQ  > E8 D3 3C 01 01 00 00 00 01 00 00 00 58 51 19 00 
{169} normal block at 0x00195A98, 32 bytes long.
 Data: < R   R   R      > 80 52 19 00 80 52 19 00 80 52 19 00 09 0C 00 00 
{168} normal block at 0x00195A48, 20 bytes long.
 Data: < Q   Q   R   Y  > E8 51 19 00 E8 51 19 00 E0 52 19 00 F8 59 19 00 
{167} normal block at 0x001959F8, 16 bytes long.
 Data: < 2=          R  > 94 32 3D 01 02 00 00 00 01 00 00 00 E0 52 19 00 
{158} normal block at 0x00195968, 80 bytes long.
 Data: <hY  hY  hY      > 68 59 19 00 68 59 19 00 68 59 19 00 CD CD CD CD 
{157} normal block at 0x00195920, 8 bytes long.
 Data: < W      > 0C 57 19 00 00 00 00 00 
{156} normal block at 0x001958B0, 52 bytes long.
 Data: < X   X   X      > B0 58 19 00 B0 58 19 00 B0 58 19 00 CD CD CD CD 
{155} normal block at 0x00195868, 8 bytes long.
 Data: < V      > F8 56 19 00 00 00 00 00 
{154} normal block at 0x001957F8, 52 bytes long.
 Data: < W   W   W      > F8 57 19 00 F8 57 19 00 F8 57 19 00 CD CD CD CD 
{153} normal block at 0x001957B0, 8 bytes long.
 Data: < V      > E4 56 19 00 00 00 00 00 
{150} normal block at 0x00195768, 8 bytes long.
 Data: <`3>     > 60 33 3E 01 00 00 00 00 
{149} normal block at 0x001952E0, 1096 bytes long.
 Data: <                > 09 0C 00 00 00 00 00 00 00 00 00 00 00 00 1A 00 
{148} normal block at 0x00195280, 32 bytes long.
 Data: < Z   Z   Z      > 98 5A 19 00 98 5A 19 00 98 5A 19 00 CD CD CD CD 
{147} normal block at 0x00195238, 8 bytes long.
 Data: < 3>     > 14 33 3E 01 00 00 00 00 
{146} normal block at 0x001951E8, 20 bytes long.
 Data: <HZ  HZ          > 48 5A 19 00 48 5A 19 00 CD CD CD CD CD CD CD CD 
{145} normal block at 0x001951A0, 8 bytes long.
 Data: < 3>  Z  > 04 33 3E 01 A8 5A 19 00 
{144} normal block at 0x00195158, 8 bytes long.
 Data: < R   Y  > E0 52 19 00 F8 59 19 00 
{143} normal block at 0x00195110, 8 bytes long.
 Data: <pO      > 70 4F 19 00 00 00 00 00 
{142} normal block at 0x00194F70, 356 bytes long.
 Data: < Q              > 10 51 19 00 00 00 00 00 00 00 00 00 00 00 00 00 
Object dump complete.

我發現這使用boost 1.47和1.50我正在使用帶有SP1的Visual Studio 2010它似乎在VS2010中。 我試圖找到一個修補程序,但沒有任何成功

謝謝你的提示!

在這兩個示例中, _CrtMemDumpAllObjectsSince在運行boost對象的析構函數之前被調用,因此沒有任何東西有機會整理自己。 仍然分配的內存幾乎肯定不是泄漏。

應該修改代碼以確保在檢查仍然分配了哪些內存之前運行析構函數:

#include <boost\regex.hpp>

int main()
{
    _CrtMemState state;
    _CrtMemCheckpoint(&state);

    {
        boost::smatch what;
    }

    _CrtMemDumpAllObjectsSince(&state);
    return 0;
}

或者,更好的是,使用_CrtSetDbgFlag函數:

http://msdn.microsoft.com/en-us/library/5at7yxcs(v=vs.100).aspx

設置_CRTDBG_LEAK_CHECK_DF標志。 在運行所有全局析構函數之后,將在程序退出時執行泄漏檢查。 這使得列出的任何物品實際上泄漏的可能性更大。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM