Puzzling Valgrind error report on InValid Free

I have a class as follows:
1
2
3
4
5
6
7
class myClass {
   double x=0;
   double y =0;
public:
   std::vector<double> xpoint1, xpoint2, ypoint1, ypoint2;
   void foo(const std::vector<double> & px1, const std::vector<double> & px2, const std::vector<double> & py1,  const std::vector<double> & py2);
...//other stuff 


I have not defined any constructors or destructors.

The function foo is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
void myClass::foo(const std::vector<double> & px1, const std::vector<double> & px2, const std::vector<double> & py1,  const std::vector<double> & py2)
{
  std::pair<double, double> min_max;

 std::cout <<"Hello from myClass 1" <<endl;
  xpoint1 = px1;
 std::cout <<"Hello from myClass 2" <<endl;
  xpoint2 = px2;
  ypoint1 = py1;
  ypoint2 = py2;
//...other stuff
}


For an array:

myClass my_arr[2][1000];

when I call say my_arr[1][0].foo(..), Valgrind gives me the following error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
....
Hello from myClass 1
==25545== Invalid free() / delete / delete[] / realloc()
==25545==    at 0x4C28040: operator delete(void*) (vg_replace_malloc.c:507)
==25545==    by 0x4018C1: __gnu_cxx::new_allocator<double>::deallocate(double*, unsigned long) (in ...)
==25545==    by 0x401823: std::_Vector_base<double, std::allocator<double> >::_M_deallocate(double*, unsigned long) (in ...)
==25545==    by 0x4035C8: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (...)
==25545==    by 0x407847: myClass::foo(std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&) (in ..)
==25545==    by 0x402748: ...)
==25545==    by 0x4031A0: ...)
==25545==    by 0x408319: main (..)
==25545==  Address 0xbff0000000000000 is not stack'd, malloc'd or (recently) free'd
==25545== 
Hello from myClass 2 


So something seems to be going wrong when I assign a vector to another
xpoint1 = px1;

If I run the program I get a segfault.

Any idea what is going wrong?
Last edited on
It seems to work on ideone.com:
http://ideone.com/vtmXTM

And stacked-crooked:
http://coliru.stacked-crooked.com/a/c98204fc048b57b7

And on my Windows 7 x64 g++ (tdm64-1) 4.9.2 that I like to use

Perhaps you use something other than g++? It might be time to update your compiler
Last edited on
Thanks for looking into this.
The snippet I presented was part of a larger program, so perhaps the bug is elsewhere.

I do use g++ 4.9.2
Perhaps the vectors that are being passed to the function might be the issue?
Could you show the parts of the code that allocate the vectors that are passed to the foo function?
Last edited on
Thanks kevinkjt2000, you were correct.

The passed vectors were part of another class otherClass. I used the code for otherClass written by someone else. The original code defined some constructors with arguments (and no argumentless constructor) . I got some warnings by the compiler, so I also added in an empty constructor with an empty body, and apparently THAT was the issue.
Changing it to otherClass() = default; solved the issue.

It is cool that Valgrind was able to give a hint as to where the problem might be.
Last edited on
Topic archived. No new replies allowed.