私はstd :: streambufから派生したクラスを持っています。なぜ、どこに漏れているのかわかりません。私が使用しているツールによれば、見た目の前の最後のポイントは、このクラスのどこかにあります(ただし、行番号は取り出せません)。私のカスタムstreambufクラスのメモリリーク
考えられるのは、このクラスは任意の数のstreambufsデータをに同期する。 (例えば、std :: coutとa ofstream.rdbuf)私はすべてのstreambufsに書き込むstd :: endlを得るまで文字列にデータを格納します
誰でも私にできることを教えてくださいリークメモリ?
これは私のヘッダーです:
#ifndef _MY_STREAM_BUF_H
#define _MY_STREAM_BUF_H
#include <iostream>
#include <algorithm>
#include <list>
#include "../../../Interface/EngineDefs.h"
namespace MyEngine
{
class MyStreamBuf : public std::streambuf
{
public:
MyStreamBuf();
~MyStreamBuf();
void AddStream(std::streambuf* sb);
void RemoveStream(std::streambuf* sb);
bool IsStreamAdded(std::streambuf* sb);
private:
std::list<std::streambuf*> mStreamBufs;
std::string mLine;
int32_t overflow(int32_t c);
int32_t sync();
};
}
#endif
のcppファイル:
#include "../../../Include/Core/Logging/MyStreamBuf.h"
namespace MyEngine
{
MyStreamBuf::MyStreamBuf() : std::streambuf()
{
}
MyStreamBuf::~MyStreamBuf()
{
mStreamBufs.clear();
mLine.clear();
}
void MyStreamBuf::AddStream(std::streambuf* sb)
{
if (sb)
mStreamBufs.push_back(sb);
}
void MyStreamBuf::RemoveStream(std::streambuf* sb)
{
if (sb)
mStreamBufs.remove(sb);
}
bool MyStreamBuf::IsStreamAdded(std::streambuf* sb)
{
if (sb)
return (std::find(mStreamBufs.begin(),mStreamBufs.end(),sb) != mStreamBufs.end());
else
return false;
}
int32_t MyStreamBuf::overflow(int32_t c)
{
int32_t r1 = 0, r2 = 0;
if (c == EOF)
return !EOF;
else
{
mLine += c;
return r1 == EOF || r2 == EOF ? EOF : c;
}
}
int32_t MyStreamBuf::sync()
{
int32_t res = 0;
for(std::list<std::streambuf*>::iterator it = mStreamBufs.begin(); it != mStreamBufs.end(); ++it)
{
if (*it)
{
(*it)->sputn(mLine.c_str(),mLine.length());
res &= (*it)->pubsync();
}
}
mLine.clear();
return res == 0 ? 0 : -1;
}
}
はあなたのストリームです*動的に割り当てられますか? remove()を呼び出すと、削除が実行されますか? – EdChum
あなたはmStreambufメンバーかMyStreamBufクラスを意味しますか?追加されたstreambufsは例えばstd :: cout.rdbufまたはofstream.rdbufです(私はクローズして削除します) – KaiserJohaan
void MyStreamBug :: RemoveStream(std :: streambuf * sb)に渡されているものはすべてsb動的に割り当てられますか?その場合は、削除する必要がありますので、削除する必要があります。エントリを削除し、ポインタを格納するときに適切な処理をしないでください。そうでない場合は、生ポインタの代わりに 'shared_ptr'を格納するコンテナを使用する方がよいでしょう。 –
EdChum