Before, for this problem also summed up: "Error:program received signal SIGSEGV, segmentation fault. (Codeblocks, C + +) "
http://blog.csdn.net/libing_zeng/article/details/55684405
The previous conclusion was that the problem occurred because the null pointer was being manipulated.
This conclusion is indeed not rigorous. Here, to sum up again.
Why this error occurs.
The reason for this is that the " invalid pointer " has been manipulated.
According to the problems encountered, experience learned that:
There are two types of "invalid pointers" that cause segment errors:
First: The pointer is not initialized.
Like what:
Material_ptr->shade (SR)
This error may be reported if the material_ptr is a null pointer (or the corresponding pointer is not a pointer to an object that has a shade () member method).
The problem of this situation is better to check. Just look at the corresponding pointer. Because this invalid pointer is usually a few strange, abnormal pointers.
Problem Resolution: Initialize the pointer.
Second: The object pointed to by the pointer has been delete.
Like what:
If you have a pointer to a class in two classes. The world class below and the Arealight class simultaneously use pointers to the rectangle class.
It is easy to go wrong in the destructor. As below, if the world's destructor deletes the object pointed to by the rectangle pointer, the Arealight destructor then deletes the object pointed to by the rectangle pointer and then reports "segmentation fault."
The problem with this situation is somewhat difficult to find, because the corresponding pointer is a normal pointer, except that the object to which the pointer is pointing has been deleted.
Because it is a normal pointer, if (OBJECT_PTR) does not work at all.
Problem Resolution: Do not do the delete operation in the destructor of the latter class (above is Arealight).