I am trying to simulate a simple 2-level paging mechanism, which dynamically creates new entries into the page tables. I am facing extra memory freed errors, when I try to run it with Valgrind and getting the error "free(): double free detected in tcache 2" on running it normally. Can someone help me where I am doing it wrong, I had made proper destructors with conditions, still it is giving such error.
I am using another function to maintain this, which will search the complete array and only then execute access_in_pmd()
In the mean time, in this program in main, I am only accessing those which are already stored and can be verified by using search function before accessing.
As I have mentioned in the main, where I am facing the problem. When I execute p.add_in_pmd(2) in the 141 line.
Because, it delete the old record in pmd (which in turn delete inner entries of other class as the result of composition) and create new array, there it is calling an extra delete[], which is the reason of the error. But I am not able to correct this error.
Yes, I am considering using std::vector, but still thought to figure out the problem with this hard-written code.
delete or free of null is fine. if you set to null after release, even if it tries to do it twice, it is harmless.
that said, its better if you trace out the logic and figure out what went wrong.
try initializing all your class pointers to nullptr on construction. Start there.