We have an existing Mutex
C++ class. It have an debug feature to track the owner thread id. So it have an member variable to store this, but because the original developer doesn't want to wast memory, so this member wrapped by an macro in header file. And This header file been included in multiple project.
// mutex.h
class Mutex
{
......
private:
#ifdef _DEBUG
int owner_thread_id_;
#end
......
}
Recently we are bitten this code, because of memory corruption caused by violating one definition rule.
So how can we implement it with following two requirement:
1. Don't wast memory because it is an utility class and wide used.
2. Don't violating the one definition rule.
Aucun commentaire:
Enregistrer un commentaire