passing a vector to a function, getting negative length and size, aka segault

This is a discussion on passing a vector to a function, getting negative length and size, aka segault within the C++ Programming forums, part of the General Programming Boards category; Hello,
I am writing a raytracer, and currently I'm working on creating a bounding volume hierarchy to accelerate the process. ...

passing a vector to a function, getting negative length and size, aka segault

Hello,

I am writing a raytracer, and currently I'm working on creating a bounding volume hierarchy to accelerate the process. To do this, I am first creating a vector that holds each of the objects in the scene, and passing this vector to the constructor for my BVH.

I am testing a scene that has only 2 objects, and so it goes to the size == 2 check. The first time it hits makeLeaf(), I segfault. I've used both gdb and valgrind, and of course it's a memory mapping error. gdb's backtrace tells me that the length of the vector I've passed in is -805305610 and the capacity is -21, and that it is inside my makeLeaf() function that the error occurs. Here's the function:

If you would like, I could post more code. Just let me know what you need to see. Also, I don't believe I'm passing a reference of my vector into my constructor, but rather the object itself. Is this an incorrect understanding of what I'm doing?

Ok, I think I got it. Would it be beneficial to change the prototype of the constructor to take a const vector<GeomObj*>& rather than the way I've done it?

Also, I just use the given std::vector.push_back() to populate the vector. As you can guess, I'm pushing back GeomObj* objects. Is there some way that the pointers or my data could have changed while being passed through the constructor or to makeLeaf() (I realize this is unlikely)? Like I said before, until I started to try and implement this BVH, my object list vector was giving me no problems at all.

Sorry, I'm not the greatest with all this memory stuff. I appreciate the help.

Do not use malloc (it will not call constructors)! Use new and delete, but even better is to not use it at all! Why do you need new/delete/malloc/free anyway?
For good measurement, some good advice:
- Do not remove parameter names - cpwiki

Questions:
- Do you need to implement your own tree? Won't std::set or std::map suffice?