1 Answer
1

Deleting a pointer twice is undefined behaviour. There's no guarantee of a segfault. You might get one if you're lucky; you might not. The code might pass all your testing and then blow up in your customer's face at the worst possible moment. See the C++ FAQ.

The same goes for dereferencing a pointer that's been deleted (the pThread->get_id() in your code).

A simple defensive technique is to set pointers to NULL as soon as they've been deleted, instead of letting them dangle. This may help catch some bugs of this type.

The above applies to pointers of any type, and not just boost::thread*.

Can't say that I agree with setting pointers to NULL. This really doesn't defend against anything; most of the time, a pointer which is deleted will be going out of scope immediately anyway, and setting this pointer to NULL doesn't do anything about other pointers which point to the same object (and which have also become invalid).
–
James KanzeSep 14 '11 at 13:30

4

@Martin: I fully agree with James about this issue. Setting pointers to NULL just masks errors. The best way to deal with a pointer you don't need anymore is to let it fall out of its scope. In that, setting your pointer to NULL resembles praying as a contraceptive: it sounds nice, but doesn't help.
–
sbiSep 14 '11 at 16:15

3

@Nic: Still another 25-30 lines? It seems your functions are way too long.
–
FredOverflowSep 14 '11 at 19:12

4

@Nicol: I rarely ever use naked pointers, and I haven't used them for managing resources in at least a decade, except as members of a class that has the sole purpose of managing that resource. There, your delete ptr_; is in the dtor, right before the closing }.
–
sbiSep 14 '11 at 19:13