Tuesday, January 8, 2013

c++ segmentation fault and undefined behaviour causes



1.I run into a issue when I call a function from a object it throws a Segmentation fault. It turns out to be the problem that I did n't make a copy of dynamic allocated elements when copying a object. The default copy constructor (and/or operator=) may not handle that correctly.

Details are explained well in this post:

http://www.cplusplus.com/forum/general/28420/

2. when you reference a item, but that item was deallocated already.
e.g.

class A {
    tcp::socket _sock;

public:
    void connect() {
        tcp::io_service _io_service;
        _sock = tcp::socket(_io_service);
    }
...
}

When connect() is called _sock is assigned with the connection socket. But _io_service is destructed when connect() finishes.

3. Destructor not virtual.

No comments:

Post a Comment