特殊成员函数过滤使用场景
用户可以通过在内存一致性动态检查命令devkit advisor dr-check之后,添加“-ff/--special-function-filter”参数的方式,实现在动态内存一致性检测过程中过滤掉存在内存一致性问题的特殊成员函数,当前支持构造函数和析构函数。
特殊成员函数过滤使用示例
- 编译源码文件,生成可执行二进制文件。
1g++ demo1.cpp -o demo1 -g -lpthread
demo1.cpp文件内容如下:
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 << "[主线程-构造] 第一步:仅初始化id = 100" << std::endl; id = 100; std::cout << "[主线程-构造] 启动子线程(此时name_ptr未初始化)" << std::endl; std::thread t(&ConstructorRace::ThreadFunc, this); t.detach(); std::cout << "[主线程-构造] 开始初始化(等待1秒),子线程会访问未初始化数据" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "[主线程-构造] 第二步:初始化name_ptr和is_ready" << std::endl; name_ptr = new std::string("name_ptr"); is_ready = true; } ~ConstructorRace() { if (name_ptr) { delete name_ptr; } std::cout << "[主线程-析构] 实例销毁" << std::endl; } void ThreadFunc() { std::cout << "[子线程] 启动,开始读取实例数据..." << std::endl; while (!is_ready) { std::cout << "[子线程] 构造未完成 → id: " << id << " | name_ptr地址: " << (void*)name_ptr << " | is_ready: " << is_ready << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(100)); } std::cout << "[子线程] 构造完成 → id: " << id << " | name: " << *name_ptr << " | is_ready: " << is_ready << std::endl; } private: int id; // 先初始化的整型 std::string* name_ptr; // 后初始化的指针 bool is_ready; // 构造完成标记 }; class DestructorRace { public: DestructorRace() : count(0), running(true) { std::cout << "[主线程] 构造函数执行:实例创建,count初始化为0" << std::endl; } ~DestructorRace() { std::cout << "[主线程] 析构函数开始执行:准备停止子线程并清理count" << std::endl; running = false; count = -999; std::cout << "[主线程] 析构函数执行完毕:count已置为" << count << std::endl; } void ThreadFunc() { std::cout << "[子线程] 开始运行,持续操作count" << std::endl; while (running) { count++; std::cout << "[子线程] 当前count值:" << count << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(50)); } std::cout << "[子线程] 退出时读取到count:" << 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 << "[主线程] 构造函数程序执行完毕" << 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 << "[主线程] 析构函数程序执行完毕" << std::endl; return 0; }
- 执行内存一致性动态检查。
devkit advisor dr-check -eb true -f /home/demo/demo1 -o /home/demo/output
- “-eb”:指定为true开启调用栈,可提高内存一致性检查结果的准确性。
- /home/demo/demo1:ELF文件路径,请根据实际情况进行修改。
- /home/demo/output:扫描报告的存放路径。
返回信息如下,从返回信息中的加粗信息可以看出有10行代码推荐修改。
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 - 查看扫描报告,发现存在5对数据竞争问题。其中,2个为成员方法和构造函数,1个为成员方法和Main函数,2个为成员方法和析构函数。图1 扫描报告1
- 再次执行内存一致性动态检查。
devkit advisor dr-check -eb true -f /home/demo/demo1 -o /home/demo/output -ff destructor,constructor
“-ff”:指定需要过滤的函数类型为destructor(析构函数)和constructor(构造函数),则扫描结果将会自动过滤对应函数的数据竞争问题。
返回信息如下,从返回信息中的加粗信息可以看出有2行代码推荐修改。
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 - 查看扫描报告,发现存在1对数据竞争问题,为成员方法和Main函数。图2 扫描报告2
父主题: 内存一致性动态检查