Valgrind claims I'm indirectly losing memory; what vexes me, is I have no idea why that would be the case.
Not sure if this is a false positive or if I just don't understand some pointer assignment or something.
Am I losing memory here? If so, why?
Valgrind report:
==24392== 21 bytes in 2 blocks are indirectly lost in loss record 1 of 3
==24392== at 0x4028699: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==24392== by 0x804B41D: Perishable::load(std::basic_fstream<char, std::char_traits<char> >&) (Perishable.cpp:42)
==24392== by 0x804C504: sict::PosApp::loadRecs() (PosApp.cpp:139)
==24392== by 0x804D58E: sict::PosApp::run() (PosApp.cpp:393)
==24392== by 0x8049337: main (milestone4.cpp:8)
Here is the line 42:
std::fstream& Perishable::load(std::fstream& stream) {
char mysku[MAX_SKU_LEN + 1];
char mynam[100];
bool mytax;
double myprice;
int myqty;
int date[5];
stream.getline(mysku, 100, ',');
sku(mysku);
stream.getline(mynam, 100, ',');
name(mynam);//bytes indirectly lost
Here is name() [fully commented]
void Item::name(char *name){
delete[] _name; //..............Just in case it points to something.
//..............Note: How could I possibly be losing memory with this line here??
int x = strlen(name); //........Grab the length of the new input
if (_name == '\0') {//..........If it was empty, ie. its the first assignment
_name = new char[x + 1];//..Allocate the space, +1 for null char
}
for (int i = 0; i < x; i++) {//.Copy
_name[i] = name[i];
}
_name[x] = '\0';//............Yeah, its manual termination. I should maybe use strcpy
}
Edit>>> here is the destructor
Item::~Item() {
std::cout << "called destructor";
delete[] _name;
_name = '\0';
}
Edit>> copy constructor and assignment operator
//Copy Constructor
Item::Item(const Item& copyfrom){
(*this) = copyfrom;
}
//Member operators
Item& Item::operator=(const Item &myitem) {
if (!myitem.isEmpty()){
init((*this), myitem._sku, myitem._name, myitem._price, myitem._taxed);
this->_quantity = myitem._quantity;
}
return (*this);
}
void Item::init(Item &obj, char const sku[], char const name[], double priced, bool taxed) {
int length = strlen(name);
int skulength = strlen(sku);
obj._price = priced;
obj._taxed = taxed;
obj._quantity = 0;
obj._name = new char[length+1]; //+1 for the null which wasn't counted. Huge pain debugging that.
for (int i = 0; i < length; i++) {
obj._name[i] = name[i];
if (i < skulength) {//redundanc
obj._sku[i] = sku[i];
}
}
obj._name[length] = '\0';
obj._sku[skulength] = '\0';
}