c++ file* 句柄泄漏_C++核心准则边译边学-P.8 不要泄漏任何资源

cab5e0a29500f3a4b5c057c75586b5d8.png

P.8: Don't leak any resources(不要泄漏任何资源)

Reason(原因)

Even a slow growth in resources will, over time, exhaust the availability of those resources. This is particularly important for long-running programs, but is an essential piece of responsible programming behavior.

即使是资源的缓慢增长,经过一定时间之后,也会最终耗尽该资源的可用性。这对长时间运行的程序特别重要,也是重要编程活动中必不可少的一部分。

Example, bad(反面事例)

void f(char* name){ FILE* input = fopen(name, "r"); // ... if (something) return; // bad: if something == true, a file handle is leaked  // ... fclose(input);}
译者注:由于某种原因,代码在fclose之前退出执行时,会导致文件无法关闭。

Prefer RAII:(使用RAII的更好示例)

void f(char* name){ ifstream input {name}; // ... if (something) return; // OK: no leak  // ...}

译者注:
资源获取即初始化(Resource Acquisition Is Initialization),或称 RAII,是一种 C++ 编程技术,它将必须在使用前请求的资源(分配的堆内存、执行线程、打开的套接字、打开的文件、锁定的互斥体、磁盘空间、数据库连接等——任何存在受限供给中的事物)的生命周期绑定与一个对象的生存期相绑定。
--https://zh.cppreference.com/w/cpp/language/raii

See also: The resource management section

参考资源管理分区。

Note(注意)

A leak is colloquially "anything that isn't cleaned up." The more important classification is "anything that can no longer be cleaned up." For example, allocating an object on the heap and then losing the last pointer that points to that allocation. This rule should not be taken as requiring that allocations within long-lived objects must be returned during program shutdown. For example, relying on system guaranteed cleanup such as file closing and memory deallocation upon process shutdown can simplify code. However, relying on abstractions that implicitly clean up can be as simple, and often safer.

泄漏也常被成为“任何没有被清理的东西”。更加重要的分类是“任何不会被清理的东西”。例如从堆中分配一个对象然后丢失了最后一个指向那个对象指针。这个规则不应该被理解为这样的要求:即长生命周期对象必须被在程序停止运转时释放。例如,利用进程停止运行时保证的关闭文件,释放内存处理的做法会简化代码,但是依赖严格清理规则的做法可以简单而且更安全。

Note(注意)

Enforcing the lifetime safety profile eliminates leaks. When combined with resource safety provided by RAII, it eliminates the need for "garbage collection" (by generating no garbage). Combine this with enforcement of the type and bounds profiles and you get complete type- and resource-safety, guaranteed by tools.

推行生命周期安全规则群可以排除资源泄漏。和RAII提供的资源安全机制一起使用的话,可以(通过不产生垃圾)排除“垃圾收集”的需求。再结合执行类型和边界规则群,你可以得到完全的类型和资源安全,一切都由工具保证。

Enforcement(执行建议)

  • Look at pointers: Classify them into non-owners (the default) and owners. Where feasible, replace owners with standard-library resource handles (as in the example above). Alternatively, mark an owner as such using owner from the GSL.关注指针:按照所有者的有无(缺省为无)进行分类。如果可能,将所有者替换为标准库中的资源句柄(例如上面的示例)。使用GSL中的owner关键字标识所有者。
  • Look for naked new and delete关注直接的new和delete
    关注
  • Look for known resource allocating functions returning raw pointers (such as fopen, malloc, and strdup)关注已知的返回原始指针的资源分配函数(例如fopen,malloc和strdup)

觉得本文有帮助?请分享给更多人。

更多更新文章,欢迎关注微信公众号【面向对象思考】

面向对象设计,面向对象编程,面向对象思考!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值