Valgrind showing invalid read of size 4 when using an erase function

41 Views Asked by At

So I am writing an assignment that utilizes a vector class that I had to make that mimics the standard Vector file for c++. In my program, I am trying to filter intervals for showing more than once. When I run through, I get the results I want, but with a RTE error and these accompanying:

==672367== Invalid read of size 4
==672367==    at 0x109D0B: Vector::erase(int*) (Vector.cpp:357)
==672367==    by 0x10A4C8: main (pa13a.cpp:79)
==672367==  Address 0x4dc00a0 is 0 bytes after a block of size 16 alloc'd
==672367==    at 0x483C583: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==672367==    by 0x10985C: Vector::operator=(Vector const&) (Vector.cpp:220)
==672367==    by 0x10A49F: main (pa13a.cpp:76)
==672367== 
Unique Timestamps:  [100, 200, 320, 480, 700]
Intervals:          [100, 120, 160, 220]
Filtered Intervals: [120, 160, 220]
Reversed Intervals: [220, 160, 120]
Combined Data:      [100, 200, 320, 480, 700, 220, 160, 120]
==672367== 
==672367== HEAP SUMMARY:
==672367==     in use at exit: 0 bytes in 0 blocks
==672367==   total heap usage: 27 allocs, 27 frees, 78,152 bytes allocated
==672367== 
==672367== All heap blocks were freed -- no leaks are possible
==672367== 
==672367== For lists of detected and suppressed errors, rerun with: -s
==672367== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

The section:

fInt = tInt; // Filtered interval = to Original Interval vector
    for (auto x = fInt.begin(); x != fInt.end(); ++x) { // pointer iteration
        while (*x == minInterval) { // value of position == the filter number
            fInt.erase(x); // erase duplicates until new number
        }
    }

Upon noticing, I tried iterating through using indexing, but got segmentation faults. I tried going through and saving individually then running through and reversing but got segmentation faults. The easiest method I can think of is this. Unless this is fine and it's my erase function in Vector class, but utilizing a test case file with similar values being inputted causes no alarms with valgrind on the class file alone.

0

There are 0 best solutions below