Question

I am currently having an issue related to:

vector<myObj> myVector;

Q1. Please tell me the difference between the following two lines:

a) myVector.push_back(*new myObj());
b) myVector.push_back(myObj());

NOTE: I realise that line a) is bad practice as it is causing a memory leak by allocating the myObj's contents dynamically before copying it into the vector and therefore can't be freed...

However, I was under the assumption that both of these lines should result in the vector containing the exact same contents, though it appears that this assumption is incorrect. The software I am currently developing RUNS fine using line a) (I know, I know this is causing a leak, please try to disregard this just for now) but crashes with various different exc_bad_access errors on line b).

Q2. can anyone explain why this might be?

EDIT: When posting this I originally assumed that my issue must be related to some difference in the resulting vector contents, however my problem was actually related to fulfilling the "Rule of Three" http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming) Thank you to @WhozCraig , @juanchopanza & @Alex Antonov for your help!

Was it helpful?

Solution 2

In line a) you're creating 2 objects and you have the following method calls:
1) Default constructor for first object
2) Copy constructor for second object

In line b) you're creating 2 objects as well but you have the following method calls:
1) Default constructor for first object
2) Copy constructor for second object
3) Destructor for first object

So obviously the line a) works because the destructor is not called. It implies that most probably in line b) you're deallocating/freeing some resource (e.g. a dynamically allocated memory) in destructor and then trying to access that resource by the second object. In that case you need to implement the copy constructor properly. E.g. you need to allocate new memory/object in copy constructor instead of simply copying a pointer to memory/object.

OTHER TIPS

what is the difference...

The difference is line a) causes a memory leak, and line b) doesn't. This is because the dynamically allocated object in a) is immediately discarded, and there is no handle to call delete on it. The vector holds and owns its elements, which in this case are copies of what you push back into it.

The software I am currently developing runs fine using line a) but crashes with various different exc_bad_access errors on line b).

It may appear to run fine, but it has a resource leak. So it is not fine by any stretch of the imagination. If you have a problem with line b) it may be because myObj manages resources and does not follow the rule of three. Line b) should work fine for a well designed class.

A1: The difference is that (a) creates an object on the heap and then dereferences it, and (b) creates a stack object.

A2: Since you need to also delete heap allocted objects you will get a memory leak. std::vector does not do this for you, since it can't know whether the passed object is allocated on the heap or the stack.

When inserting objects to an vector, it will be copied. If you want to keep heap allocated objects, you need to define it as such:

std::vector<*myObj> myVector;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top