Special Member Function Filtering
You can add the -ff/--special-function-filter option after the devkit advisor dr-check command to filter out special member functions that have memory consistency issues during dynamic memory consistency check. Currently, constructors and destructors are supported.
Example
- Compile the source file to generate an executable binary file.
1g++ demo1.cpp -o demo1 -g -lpthread
Content of the demo1.cpp file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#include <iostream> #include <thread> #include <chrono> #include <string> class ConstructorRace { public: ConstructorRace() { std::cout << "[Main thread - constructor] Step 1: Initialize only id = 100." << std::endl; id = 100; std::cout << "[Main thread - constructor] Start the sub thread (name_ptr is not initialized yet)." << std::endl; std::thread t(&ConstructorRace::ThreadFunc, this); t.detach(); std::cout << "[Main thread - constructor] Start initialization (waiting for 1 second). The sub thread accesses uninitialized data." << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "[Main thread - constructor] Step 2: Initialize name_ptr and is_ready." << std::endl; name_ptr = new std::string("name_ptr"); is_ready = true; } ~ConstructorRace() { if (name_ptr) { delete name_ptr; } std::cout << "[Main thread - destructor] Destruct the instance." << std::endl; } void ThreadFunc() { std::cout << "[Sub thread] starts and reads the instance data..." << std::endl; while (!is_ready) { std::cout << "[Sub thread] Constructing not complete. → id: " << id << " | name_ptr address: " << (void*)name_ptr << " | is_ready: " << is_ready << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(100)); } std::cout << "[Sub thread] Constructing completes. → id: " << id << " | name: " << *name_ptr << " | is_ready: " << is_ready << std::endl; } private: int id; // Earlier initialized integer std::string* name_ptr; // Later initialized pointer bool is_ready; // Construction completion flag }; class DestructorRace { public: DestructorRace() : count(0), running(true) { std::cout << "[Main thread] Constructor execution: Create an instance and initialize count to 0." << std::endl; } ~DestructorRace() { std::cout << "[Main thread] The destructor starts to be executed. Prepare to stop the sub thread and clear the count." << std::endl; running = false; count = -999; std::cout << "[Main thread] The destructor execution is complete. count is set to " << count << std::endl; } void ThreadFunc() { std::cout << "[Sub thread] starts to run and continuously operates the count." << std::endl; while (running) { count++; std::cout << "[Sub thread] current count value: " << count << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(50)); } std::cout << "[Sub thread] reads the count when exiting: " << count << std::endl; } private: int count; bool running; }; int main() { ConstructorRace* obj = new ConstructorRace(); std::this_thread::sleep_for(std::chrono::seconds(2)); delete obj; std::cout << "[Main thread] The constructor program execution completed" << std::endl; DestructorRace* obj2 = new DestructorRace(); std::thread t(&DestructorRace::ThreadFunc, obj2); std::this_thread::sleep_for(std::chrono::milliseconds(200)); delete obj2; t.join(); std::cout << "[Main thread] The destructor program execution completed" << std::endl; return 0; }
- Perform dynamic memory consistency check.
devkit advisor dr-check -eb true -f /home/demo/demo1 -o /home/demo/output
- -eb: If this option is set to true, the call stack is enabled, improving the accuracy of memory consistency check results.
- /home/demo/demo1: ELF file path. Replace the example path with the actual one.
- /home/demo/output: Scan report path.
The following information in bold indicates that 10 lines of code need to be modified.
Executing dynamic memory consistency check task, please wait... The pid of the memtracer process is 2140377 Scanned time: 2026/02/24 08:45:16 Configuration: ELF filepath: /home/demo/demo1 Scan source code path: None Generate report path: /home/demo/output Generate report type: all Task Timeout Interval: The timeout period is not set. Log level: info Summary: There are 10 recommended code lines to modify. For the detailed information on multi-thread races, please check: /home/demo/output/dr-check_demo1_20260224084516_5b68.json /home/demo/output/dr-check_demo1_20260224084516_5b68.html /home/demo/output/dr-check_demo1_20260224084516_5b68.csv - View the scan report. It is found that there are five data race issues. Among which, two are related to member methods and constructors, one is related to a member method and main function, and the other two are related to member methods and destructors.Figure 1 Scan report 1
- Perform dynamic memory consistency check again.
devkit advisor dr-check -eb true -f /home/demo/demo1 -o /home/demo/output -ff destructor,constructor
-ff: If the function types to be filtered out are destructor and constructor, the scan result automatically filters out the data race issues of the functions.
The following information in bold indicates that two lines of code need to be modified.
Executing dynamic memory consistency check task, please wait... The pid of the memtracer process is 2141259 Scanned time: 2026/02/24 08:46:47 Configuration: ELF filepath: /home/demo/demo1 Scan source code path: None Generate report path: /home/demo/output Generate report type: all Task Timeout Interval: The timeout period is not set. Log level: info Summary: There are 2 recommended code lines to modify. For the detailed information on multi-thread races, please check: /home/demo/output/dr-check_demo1_20260224084647_66fb.json /home/demo/output/dr-check_demo1_20260224084647_66fb.html /home/demo/output/dr-check_demo1_20260224084647_66fb.csv - View the scan report. It is found that there is a data race issue, which is related to member methods and main functions.Figure 2 Scan report 2
Parent topic: Dynamic Memory Consistency Check