Hi, I'm working on a project for a class and have to use a dynamically allocated 2D array to hold and manipulate an image. I am having issues actually doing this and whenever my program ends, I get a **glibc detected** ./driver: double free or corruption error. I have tracked the error down to my destructor and have a feeling that I'm doing it wrong, but I am horribly confused by which order I need to deallocate the memory.
Here is my constructor for the class I am using:
What are your copy constructor and copy assignment operator?
Also, check your accesses: your data structure holds cols arrays of rows elements in each. If you're ever accessing it using the more traditional [row] [col] indexing, you may be corrupting the heap then.
Also, why arrays?
As far as the copy assignment operator goes, that hasn't been written yet, but if it had, it would look similar to the copy constructor. I'm trying to test functionality as I add it so that I don't get a huge group of errors when the program is complete.
Using arrays was part of the assignment. I know that when I'm introduced to a better way to do this, I will not look back because this is proving to be quite the pain.
==4162== Memcheck, a memory error detector
==4162== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==4162== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==4162== Command: ./test
==4162== HEAP SUMMARY:
==4162== in use at exit: 0 bytes in 0 blocks
==4162== total heap usage: 45 allocs, 45 frees, 1,920 bytes allocated
==4162== All heap blocks were freed -- no leaks are possible
Weird, I just completely discarded the code I was using before and rewrote the constructor and destructor and it works now... I think I just needed a fresh start or I had some problem existing elsewhere in the code. Thanks for taking a look at it.
I'm not sure if this was part of the spec, but on a modern processer allocating an image a row at a time is noticeably slower than allocating a single buffer and computing offsets into the single buffer. IMHO it's also simpler to code up and implement. Also you should likely fix the signature on operator=(Image rhs) to operator(Image && rhs) or just get rid of this function entirely.