You did not implement the copy constructor and copy assignment operator (see rule three). This results in a shallow copy of the pointers in your vector, which leads to double deletion and approval. EDIT: Double deletion is undefined behavior, so VS and gcc are correct here, they are allowed to do whatever they want.
Usually, when you implement a destructor with non-trivial behavior, you also need to write or disable copy building and copy assignment.
However, in your case, do you really need to store the elements by pointer? If not, just save them by value and this will fix the problem. Otherwise, if you need pointers, use shared_ptr (from your compiler or boost) instead of raw pointers to save you the trouble of writing your own destructor / copy methods.
EDIT: additional note about your interface: interfaces that transfer ownership of the ones passed in pointers can cause confusion on the part of people using your class. If someone passed an int address not allocated to the heap, then your destructor will fail anyway. Itβs better to either accept the value if possible, or, if possible, clone your own new call to the add function in the add function.
source share