Segfaulting means trying to manipulate a memory location that should not be accessible to the application.
This means that your problem can arise from three cases:
- Trying to do something with a pointer pointing to NULL;
- Trying to do something with an uninitialized pointer;
- Trying to do something with a pointer pointing to a now deleted object;
1) it is easy to check, so I assume that you already do this when you invalidate pointers in a vector. If you do not perform checks, do this before calling the delete. This will indicate when you are trying to delete an object twice. 3) cannot happen if you set NULL to a pointer in a vector.
2) may also occur. In your case, you are using std :: vector, right? Make sure that implicit vector manipulations (for example, reallocating the internal buffer when it is not big enough) do not damage your list.
So, first check that you are deleting NULL pointers (note that delete (NULL) will not throw! This is the standard and correct behavior!) - in your case, you should not get to the point of trying to delete (NULL). Then, if this does not happen, make sure that you do not fill the vector with pointers pointing to the basket. For example, you should make sure that you are familiar with [Remove-Erase idiom] [1].
Now that you have added the code, I think I see the problem:
int tid = _myVec.size();
You use the index as identifiers.
Now it depends on how you delete your objects. (please show it for a more complete answer)
- You just pointed to a NULL pointer.
- You are removing the pointer from the vector.
If you only do 1) then it should be safe (unless you worry about a vector that grows and never gets free, and identifiers are not reused).
If you do 2. then this is all wrong: every time you delete an object from a vector, the whole object remains after the position of the deleted object is dropped by one. Invalid invalid id / index.
Make sure you are consistent at this point; this is definitely a source of errors.
Klaim
source share