22nd Jul 2022, 8:56 AM
Victor Diogo
1 Answer
+ 6
texture importTex() { texture tex; tex.data = new Byte[55]; return tex; } ... tex = importTex(); When the function returns it uses the assignment operator to copy the internal data of the texture to the variable tex within main. At this point, both tex inside the function contain a pointer to the data on the heap as well as the tex variable inside main. function-tex now has to destruct so it calls delete on the data, the same data that main-tex is pointing to. Main-tex's data is now pointing to invalid data. Main finishes and main-tex has to delete its data now, calling delete again on an already deleted piece of memory. This is called a double free ( as the program reports ) and it crashes the program ( in this case ). Whenever you implement a destructor of any kind, you most likely have to implement other functions as well like an assignment operator and copy constructor to name a few. More on that here: https://en.cppreference.com/w/cpp/language/rule_of_three
22nd Jul 2022, 10:22 AM
Dennis
Dennis - avatar