gpt4 book ai didi

delphi - ARC : is there a memory leak in TNetEncoding. 获取Base64Encoding?

转载 作者:行者123 更新时间:2023-12-03 14:43:45 25 4
gpt4 key购买 nike

下面是TNetEncoding.GetBase64Encoding的Delphi原始源代码。但我怀疑万一AtomicCmpExchange(Pointer(FBase64Encoding), Pointer(LEncoding), nil) <> nil会出现内存泄漏

 TNetEncoding = class
private
class var
FBase64Encoding: TNetEncoding;

class function TNetEncoding.GetBase64Encoding: TNetEncoding;
var
LEncoding: TBase64Encoding;
begin
if FBase64Encoding = nil then
begin
LEncoding := TBase64Encoding.Create;
if AtomicCmpExchange(Pointer(FBase64Encoding), Pointer(LEncoding), nil) <> nil then
LEncoding.Free;
{$IFDEF AUTOREFCOUNT}
FBase64Encoding.__ObjAddRef;
{$ENDIF AUTOREFCOUNT}
end;
Result := FBase64Encoding;
end;

我认为应该写成:

class function TNetEncoding.GetBase64Encoding: TNetEncoding;
var
LEncoding: TBase64Encoding;
begin
if FBase64Encoding = nil then
begin
LEncoding := TBase64Encoding.Create;
if AtomicCmpExchange(Pointer(FBase64Encoding), Pointer(LEncoding), nil) <> nil then
LEncoding.Free
{$IFDEF AUTOREFCOUNT}
!!!ELSE!!! FBase64Encoding.__ObjAddRef;
{$ENDIF AUTOREFCOUNT}
end;
Result := FBase64Encoding;
end;

我错了吗?

最佳答案

你是对的。当 if 语句计算结果为 true 时,代码失败。当发生这种情况时,另一个线程已设法首先分配单例,并且已经增加了引用计数。引用计数不应再次增加。

您应该提交错误报告。

关于delphi - ARC : is there a memory leak in TNetEncoding. 获取Base64Encoding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com