-1

与我之前的问题相关,我假设内存泄漏发生在 std::string 中,但深入研究后,我得到了一些奇怪的结果。让我们开始:

考虑到我们有一个全球

static volatile std::wostringstream *Log = nullptr;

在 WriteToLog() 函数中,我们有以下代码:

    std::wostringstream* new_log = new std::wostringstream(std::ios::in | std::ios::out);
    new_log->imbue(CConsumer::GetUtf8Locale());

    std::wostringstream* old_log = (std::wostringstream*)Log;

    while((std::wostringstream *)::InterlockedCompareExchangePointer((PVOID volatile *)&Log, new_log, (PVOID)old_log) != new_log)
    {
        ::SleepEx(10, FALSE);
    }

    std::string logtext(Hooker::Utf16ToUtf8(old_log->str()));

它利用专有的:

    static std::locale FORCEINLINE GetUtf8Locale()
    {
        static std::unique_ptr<std::codecvt_utf8_utf16<wchar_t>> code_cvt(new std::codecvt_utf8_utf16<wchar_t>(std::codecvt_mode::generate_header | std::codecvt_mode::little_endian));
        return std::locale(std::locale(), code_cvt.get());
    }

由于日志事件偶尔发生,它会产生巨大的内存泄漏(从最初的 5MB/500 个句柄在几分钟内跃升至 200MB/300,000 个句柄)。

以前,我假设它是与 std::string 相关的泄漏,但是,使用 Visual Studio Profiler,它表明所有泄漏都是由 GetUtf8Locale() 引起的。

有人可以帮我解决这个问题吗?

4

1 回答 1

0

这个答案是错误的,我忽略了 InterlockedCompareExchangePointer 的“比较”部分。但是,我认为这是朝着正确方向迈出的一步。

所以我们从一个Log成员指向的对象开始0x87654321。然后两个线程调用WriteToLog

Thread1                                Thread2
...new_log=new ... 
(now new_log=0x15331564)
                                       ...new_log=new ...
                                       (now new_log=0x25874963)
...old_log=Log;
(now old_log=0x87654321)
                                       ...old_log=Log;
                                       (now old_log=0x87654321)

InterlockedCompareExchangePointer
(now new_log=0x87654321)
(now Log=0x15331564)

                                       InterlockedCompareExchangePointer
                                       (now new_log=0x15331564)
                                       (now Log=0x25874963)
...stuff...                            ...stuff...
delete old_log 
(now 0x87654321 is deleted)
                                       delete old_log
                                       (now 0x87654321 is deleted TWICE!)

并且Log会员指向0x25874963,所以... 日志0x15331564泄露了!

您的使用InterlockedCompareExchangePointer不正确。我认为这是正确的,具体取决于您未显示的代码。

std::wostringstream* new_log = new std::wostringstream(std::ios::in | std::ios::out);
new_log->imbue(CConsumer::GetUtf8Locale());

std::wostringstream* old_log = ::InterlockedExchangePointer((PVOID volatile *)&Log.get(), new_log);

std::string logtext(Hooker::Utf16ToUtf8(old_log->str()));

delete old_log;
于 2015-03-31T17:21:05.030 回答