c++ - Why does my destructor only crash my code after it's called by its parent destructor? -


i have doubly threaded linked list made of winery elements, winery being class own data fields. problem having winery's destructor:

winery::~winery() {         delete[] name;         delete[] location; } 

i received "double free or corruption (out): 0x0000000000402940" error in linux, , received error in windows visual studio:

enter image description here

so put breakpoint on winery's destructor , found being called every time referenced winery object. strangely, time error thrown when winery's destructor called because list's destructor had been called, if winery had been called 2 or 3 times. know why code didn't crash second time winery's destructor called when wasn't called parent destructor.



as side note/question: able solve issue of memory leakage removing winery's destructor , putting calls in parent destructor this:

list::~list() {     (node* next; headbyname; headbyname = next)     {         next = headbyname->nextbyname;         delete[] headbyname->item.getname();         delete[] headbyname->item.getlocation();         delete headbyname;     } } 

winery type name 'item' , getname() , getlocation() functions of winery return cstrings. works it's counter-intuitive, , seems inefficient. bad code? should using `winery's destructor , overriding when it's called somehow?

your linked list implementation flawed. storing winery , not pointer it. means without proper deep copy constructor data not duplicated, pointer values. cause destruction.

either modify linked list store pointers (the better way) or provide copy constructor duplicates data. latter cause more memory allocations , slower, not allow change values every place specific winery stored have same value.

when not using pointers happens this:

{     winery foo("name"); // constructor copies name new buffer     winery bar = foo; // new copy created, shallow copy of data. means bar.name = foo.name!     ... } // automatic destruction of objects, both have same pointers resulting in double free 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -