Can't push_back a class containing an ofstream pointer to a vector

kaets

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.

R Sahu

The first problem that your code suffers from is that you are not following The Rule of Three.

However, your problem is deeper than what is suggested to follow The Rule of Three.

Say your class had a different member variable than std::ofstream*.

class Test {
  public:
    Test(int in) : ptr(new int(in)) {}
    ~Test();
private:
    int* ptr;
}

You can update that class to follow the rule of three by making the sure that you do the right thing in the copy constructor and the copy assignment operator. In both of those, you'll have to use something along the lines of:

ptr = new int(*copy.ptr);

That works for most types. However, that does not work for std::ofstream since std::ofstream does not have a copy constructor or a virtual function that can return a pointer by cloning the object.

In your case, neither of the following is an option.

ofstr = new ofstream(*copy.ofstr);
ofstr = copy.ofstr->clone();

To get around that problem, you can use std::shared_ptr<std::ofstream>.

class Test {
  public:
    Test(std::string path);
    ~Test();
  private:
    std::shared_ptr<std::ofstream> ofstr;
}

When you do that, not only do you fix your problem but also, you can let the compiler generated destructor, copy constructor, and copy assignment operator do the right thing. Your class definition can be simplified to:

class Test {
  public:
    Test(std::string path);
  private:
    std::shared_ptr<std::ofstream> ofstr;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't push_back a class into an object vector inside a for loop

From Dev

Why can't I push_back to a vector of const elements?

From Dev

Can't push_back an unique_ptr in a vector

From Dev

push_back a pointer to a vector of pointers

From Dev

string Vector push_back failing in class

From Dev

Implementing push_back(T&& c) in custom Vector<T> class

From Dev

Pointer to object in a class: push_back and pointers conflicts

From Dev

vector push_back doesn't work c++

From Dev

How to reconstruct non-pointer class member containing vector?

From Dev

std vector push_back' : 2 overloads have no legal conversion for 'this' pointer error

From Dev

unable to push back a pointer into vector

From Dev

vector push_back in STL?

From Dev

Vector and push_back() behavior

From Dev

Unable to push_back to vector

From Dev

making boost::ptr_vector container class push_back function

From Dev

How can I pass a pointer to the constructor of the object when instantiating the object with push_back()?

From Dev

Inserting the data ,containing a pointer, to vector

From Dev

Can C++ std::vector handle push_back from multithreads at the same time?

From Dev

vector::push_back(myVec) on to heap. Can you avoid myVec header information on stack?

From Dev

can i check content of values that have been push_back to a vector?

From Dev

Can C++ std::vector handle push_back from multithreads at the same time?

From Java

std::vector::push_back() doesn't compile on MSVC for an object with deleted move constructor

From Dev

C++ STD Vector push_back doesn't seem to work

From Dev

Vector containing function of a class

From Dev

vector push_back zero into an empty vector

From Dev

push_back a vector of vectors into a vector

From Dev

Can one have a vector containing class templates in c++?

From Dev

How to use push_back with double* pointer

From Dev

how to format push_back on a pointer?

Related Related

  1. 1

    Can't push_back a class into an object vector inside a for loop

  2. 2

    Why can't I push_back to a vector of const elements?

  3. 3

    Can't push_back an unique_ptr in a vector

  4. 4

    push_back a pointer to a vector of pointers

  5. 5

    string Vector push_back failing in class

  6. 6

    Implementing push_back(T&& c) in custom Vector<T> class

  7. 7

    Pointer to object in a class: push_back and pointers conflicts

  8. 8

    vector push_back doesn't work c++

  9. 9

    How to reconstruct non-pointer class member containing vector?

  10. 10

    std vector push_back' : 2 overloads have no legal conversion for 'this' pointer error

  11. 11

    unable to push back a pointer into vector

  12. 12

    vector push_back in STL?

  13. 13

    Vector and push_back() behavior

  14. 14

    Unable to push_back to vector

  15. 15

    making boost::ptr_vector container class push_back function

  16. 16

    How can I pass a pointer to the constructor of the object when instantiating the object with push_back()?

  17. 17

    Inserting the data ,containing a pointer, to vector

  18. 18

    Can C++ std::vector handle push_back from multithreads at the same time?

  19. 19

    vector::push_back(myVec) on to heap. Can you avoid myVec header information on stack?

  20. 20

    can i check content of values that have been push_back to a vector?

  21. 21

    Can C++ std::vector handle push_back from multithreads at the same time?

  22. 22

    std::vector::push_back() doesn't compile on MSVC for an object with deleted move constructor

  23. 23

    C++ STD Vector push_back doesn't seem to work

  24. 24

    Vector containing function of a class

  25. 25

    vector push_back zero into an empty vector

  26. 26

    push_back a vector of vectors into a vector

  27. 27

    Can one have a vector containing class templates in c++?

  28. 28

    How to use push_back with double* pointer

  29. 29

    how to format push_back on a pointer?

HotTag

Archive