How to avoid violating one definition rule in this case?

We have an existing Mutex C++ class. It has a debug feature to track the owner thread id. So it has a member variable to store this, but because the original developer doesn’t want to waste memory, this member is wrapped by a macro in a header file.

This header file has been included in multiple projects, some binaries are developed by other departments, so for those libraries we cannot control the compile flag switch, and we do not want this strong coupling.

// mutex.h
class Mutex
{
    ......
private:
    #ifdef _DEBUG
    int owner_thread_id_;
    #end
    ......
}

Recently we were bitten by this code, because of memory corruption caused by violating the one definition rule.

So how can we implement it with following two requirements:

  1. Don’t waste memory because it is a utility class and wide used.
  2. Don’t violating the one definition rule.

6

Simply. Don’t mix code built with debug with code built without.

If that class is exported by library you link against, you either need to:

  • Have debug and release version of the library. That’s what one always does on Windows where even the standard runtime has such two versions and they are incompatible, so there is no other way in most cases. This is also the only option if it’s third party library you can’t fix.
  • Create debug flag specific to the library and keep a configuration header around setting it as appropriate for the way you built the library.
  • Leave the field in unconditionally and only make the check conditional (and make sure the all relevant functionality is in functions that get compiled into the library, i.e. defined in .cpp files).
  • Convert the class to use the PImpl idiom. Then the private fields don’t affect ABI.

Ad Edit 1: To prevent linking against the library compiled with different flags than the client code you need to make some symbols be called differently depending on the flag (_DEBUG).

In C++ most suitable method seems to be putting some code in a subnamespace, make that subnamespace be called differently in debug and release configuration, i.e. something like

#ifdef _DEBUG
namespace library_internal_debug
#else
namespace library_iternal_release
#endif

and then import the symbols to the main namespace with similarly conditional using:

namespace library {
#ifdef _DEBUG
    using library_iternal_debug::stuff;
#else
    using library_iternal_release::stuff;
#endif

That way the client will use library::stuff in either configuration, but the actual symbol names will differ, so linking against incorrect build will fail. And unlike renaming with preprocessor this will be properly scoped.

You only need to apply it to one or few important symbols.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật