【发布时间】:2016-11-09 16:22:25
【问题描述】:
我从std::basic_ostream 派生,因为我需要更改创建底层文件缓冲区的方式。 (我需要一个可以查询的唯一文件名,以便在文件关闭时可以将其删除;即,不能使用std::tmpnam 或std::tmpfile。)我已经尝试过(见下文),但是我在销毁时遇到分段错误。
导致分段错误的原因是什么? 似乎我没有为文件缓冲区分配内存,或者我将其删除了两次。
// Compile with
// clang++ -std=c++14 -stdlib=libc++ main.cpp -o tmpFile
//
#include <iostream>
#include <fstream>
#include <string>
#include <streambuf>
using namespace std;
template< class charT, class traits=char_traits<charT> >
class FILE_streambuf : public std::basic_streambuf<charT, traits> {
public:
FILE_streambuf(std::string& filename)
{
filename = "chicken";
buffer_ = fopen(filename.c_str(), "wx");
}
virtual ~FILE_streambuf(){
fclose(this->buffer_);
}
FILE* buffer_;
};
template< class charT, class traits=char_traits<charT> >
class tmp_ofstream : public basic_ostream<charT, traits>{
public:
tmp_ofstream()
: filename_("")
{
try{
this->rdbuf(new FILE_streambuf<charT, traits>(filename_));
} catch (const std::exception& e){
throw e;
}
}
~tmp_ofstream(){
delete this->rdbuf();
remove( filename_.c_str() );
}
std::string filename() { return this->filename_; };
std::string filename_;
};
int main(){
tmp_ofstream<char> tmpStream;
cout << "tmpStream has filename: " << tmpStream.filename();
cout << "\n-----------------------------------------\n";
return 0;
}
【问题讨论】:
-
因为你应该永远
free()你new。new->delete,malloc->free -
感谢您的建议。当我将
free更改为delete时,我仍然遇到相同的分段错误。 :( -
提供的源代码没有产生描述的错误,你能提供更多细节吗?
-
你为什么在
~tmp_ofstream()中调用remove( filename_.c_str() );? -
std::basic_ostream不应该有默认构造函数。目前还不清楚你的程序是如何编译的。让它tmp_ofstream() : filename_(""), basic_ostream<charT, traits>(nullptr)seems to work for me
标签: c++ c++11 segmentation-fault libc++