当前位置:网站首页>D manual destruction may violate memory security
D manual destruction may violate memory security
2022-06-25 00:13:00 【fqbqrr】
Destructor The purpose of this paper is to describe object Life span end Cleaning during . allow @safe The code manually calls... Before this point Destructor , Then continue to use Analytic object Will destroy RAII memory management :
module app;
import core.stdc.stdlib : malloc, free;
struct UniqueInt {
private int* target;
this(const(bool) doCreate) scope @trusted nothrow @nogc {
target = cast(int*) malloc(int.sizeof);
*target = 5;
}
@disable this(this);
@disable this(ref typeof(this));
@disable ref typeof(this) opAssign(ref typeof(this));
void withBorrow(void delegate(scope int*) @safe action) @safe {
action(target); }
~this() scope @trusted nothrow @nogc {
if(target !is null) {
free(target);
target = null;
}
}
}
//
UniqueInt unique;
shared static this() {
unique = true; }
void main() @safe {
import std.stdio: writeln;
unique.withBorrow((scope int* borrowed) @safe {
writeln(*borrowed);
destroy(unique);
writeln(*borrowed); // Use after release
});
}
I think the reasonable solution is : No matter ~this() What are the properties , Make call __dtor,__xdtor or Manual deconstruction by @system operation .
In order to be in Security Function is automatically created T Type object ,T The destructor must be Safe or trusted Of .
At present, the destructor is composed of __dtor Member functions embody , namely Destructor declaration The properties of are the same as the generated __dtor The properties of member functions are the same .
There are some type Need to define Destructor To do something unsafe , Mainly Release In object Carefully limit Of memory .
This creates the following difficulties :1. If type Make the destructor @trusted, It means Security The code is also free to manually call __dtor(). Subsequent use of such objects will Breach of safety .2. If type Choose to use @system Destructor , Then the security code cannot be created Such objects .
At first glance , You need a way to tell the compiler " Only when implicitly calling Destructor when , It's trustworthy ".
边栏推荐
- It's 2022, and you still don't know what performance testing is?
- Reservoir dam safety monitoring
- Hello C (III) - pointer
- Current situation analysis and development trend forecast report of global and Chinese acrylonitrile butadiene styrene industry from 2022 to 2028
- Hello C (IV) -- pointer and function
- 人体改造 VS 数字化身
- canvas螺旋样式的动画js特效
- JDBC - database connection
- C WinForm maximizes occlusion of the taskbar and full screen display
- 从数字化过渡到智能制造
猜你喜欢

UE4 WebBrowser chart cannot display problems

Today's sleep quality record 79 points

无人驾驶: 对多传感器融合的一些思考

Power application of 5g DTU wireless communication module

Laravel framework knowledge

水库大坝安全监测

Eye gaze estimation using webcam

I suddenly find that the request dependent package in NPM has been discarded. What should I do?

Tape SVG animation JS effect

Svg+js keyboard control path
随机推荐
Global and Chinese 3-Chlorobenzaldehyde industry operation mode and future development trend report 2022 ~ 2028
时间统一系统
How to delete the entire row with duplicate items in a column of WPS table
Sitelock helps you with the top ten common website security risks
C程序设计专题 15-16年期末考试习题解答(上)
离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
linux 系统redis常用命令
从数字化过渡到智能制造
Scala IO serialization and deserialization
Hello C (two) -- use of bit operation
Report on operation mode and future development trend of global and Chinese propenyl isovalerate industry from 2022 to 2028
C# Winform 最大化遮挡任务栏和全屏显示问题
5G dtu无线通信模块的电力应用
Creative SVG ring clock JS effect
OTT营销之风正盛,商家到底该怎么投?
Ten commandments of self-learning in machine learning
The living standards of ordinary people
Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)
The new employee of the Department after 00 is really a champion. He has worked for less than two years. The starting salary of 18K is close to me when he changes to our company
Scala IO reads data from URLs and other data sources