I've come across a weird segfault while trying to push a class containing a pointer of an ofstream to a vector. I have narrowed the problem down to the simplest test case possible:
Test.h
#ifndef __TEST_
#define __TEST_
#include <fstream>
#include <string>
class Test {
public:
Test(std::string path);
~Test();
private:
ofstream* ofstr;
}
#endif
Test.cpp
#include "Test.h"
Test::Test(std::string path) {
ofstr = new ofstream(path, std::ios::app);
}
Test::~Test() {
delete ofstr;
}
main.cpp
#include <vector>
#include "Test.h"
int main() {
Test test("hello.txt");
std::vector<Test> vec;
vec.push_back(test); // segfaults
}
I think that the segfault has to do with the destructor for Test, but I'm not sure why. The segfault occurs when I use emplace_back as well.