☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

避免异步终止共享对象的生命周期

16.6 ID_illLifetime
目录 › next › previous

共享对象的使用情况在异步过程中是难以掌控的,贸然终止共享对象的生命周期往往会导致标准未定义的行为。

示例:

void foo(mutex* pm) {
    lock(pm);
    ....
}

void bar() {
    mutex m;
    beginThread(foo, &m);   // Non-compliant, ‘m’ is a local object
    beginThread(foo, &m);   // Non-compliant
}

设例中 beginThread 创建执行 foo 函数的线程,bar 与 foo 是异步过程,共享对象 m 在 bar 返回后失效,如果 foo 继续访问共享对象就会出错,bar 应等待线程执行完毕或调整共享对象的生命周期。

相关

ID_asynchronousTermination ID_localAddressFlowOut ID_danglingDeref

依据

ISO/IEC 14882:2011 30.4.1.2.1(5)-undefined ISO/IEC 14882:2017 33.4.3.2.1(5)-undefined

参考

SEI CERT CON31-C SEI CERT CON50-CPP
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.