gpt4 book ai didi

delphi - 为什么没有发生MemoryLeak或AccessViolation?

转载 作者:行者123 更新时间:2023-12-02 10:45:02 24 4
gpt4 key购买 nike

我发现了两个让我好奇的案例。

首先是为什么使用JSON库时不会出现内存泄漏。

如果 GetValue 返回未实现接口(interface)的 TJSONValue。为什么我的“如果”没有引发内存泄漏?

procedure TFrmApp.btnJSONClick(Sender: TObject);
var
JSONObject: TJSONObject;
JSONStr: string;
begin
JSONStr := '{"colors":[{"name":"red", "hex":"#f00"}]}';

JSONObject := TJSONObject.ParseJSONValue(JSONStr) as TJSONObject;
try

// If GetValue returns a TJSONValue that does not implement an interface.
// Why does my "if" not raise a memory leak?
if JSONObject.GetValue('colors') <> nil then
memo.Lines.Add('Colors exist.')
else
memo.Lines.Add('Colors not found.');

finally
JSONObject.Free;
end;
end;

第二个是为什么在访问 Cd 的字段时不会发生访问冲突。

如果提供数据的 Cd 已从内存中释放。为什么没有发生访问冲突?

procedure TFrmApp.btnDataSetClick(Sender: TObject);
var
Cds: TClientDataSet;
begin
Cds := TClientDataSet.Create(Self);
try
Cds.Data := Self.GetData;

// If the Cds that provided the Data has been released from memory.
// Why does not access violation occur?
memo.Lines.Add('Name: ' + Cds.FieldByName('VendorName').AsString);
memo.Lines.Add('City: ' + Cds.FieldByName('City').AsString);

finally
Cds.Free;
end;
end;

function TFrmApp.GetData: OleVariant;
var
Cds: TClientDataSet;
begin
Cds := TClientDataSet.Create(Self);
try
Cds.LoadFromFile(TDirectory.GetCurrentDirectory + '\data.xml');

Result := Cds.Data;
finally
Cds.Free;
end;
end;

示例项目:

.DPR

program TestMemoryLeak;

uses
Vcl.Forms,
uApp in 'uApp.pas' {FrmApp};

{$R *.res}

begin
ReportMemoryLeaksOnShutdown := true;
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TFrmApp, FrmApp);
Application.Run;
end.

uAPP.pas

unit uApp;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.JSON, REST.Json, Datasnap.DBClient, Data.DB,
System.IOUtils;

type
TFrmApp = class(TForm)
btnJSON: TButton;
btnDataSet: TButton;
memo: TMemo;
procedure btnJSONClick(Sender: TObject);
procedure btnDataSetClick(Sender: TObject);
private
{ Private declarations }
function GetData: OleVariant;
public
{ Public declarations }
end;

var
FrmApp: TFrmApp;

implementation

{$R *.dfm}


procedure TFrmApp.btnJSONClick(Sender: TObject);
var
JSONObject: TJSONObject;
JSONStr: string;
begin
JSONStr := '{"colors":[{"name":"red", "hex":"#f00"}]}';

JSONObject := TJSONObject.ParseJSONValue(JSONStr) as TJSONObject;
try

// If GetValue returns a TJSONValue that does not implement an interface.
// Why does my "if" not raise a memory leak?
if JSONObject.GetValue('colors') <> nil then
memo.Lines.Add('Colors exist.')
else
memo.Lines.Add('Colors not found.');

finally
JSONObject.Free;
end;
end;

procedure TFrmApp.btnDataSetClick(Sender: TObject);
var
Cds: TClientDataSet;
begin
Cds := TClientDataSet.Create(Self);
try
Cds.Data := Self.GetData;

// If the Cds that provided the Data has been released from memory.
// Why does not access violation occur?
memo.Lines.Add('Name: ' + Cds.FieldByName('VendorName').AsString);
memo.Lines.Add('City: ' + Cds.FieldByName('City').AsString);

finally
Cds.Free;
end;
end;

function TFrmApp.GetData: OleVariant;
var
Cds: TClientDataSet;
begin
Cds := TClientDataSet.Create(Self);
try
Cds.LoadFromFile(TDirectory.GetCurrentDirectory + '\data.xml');

Result := Cds.Data;
finally
Cds.Free;
end;
end;

end.

uApp.dfm

object FrmApp: TFrmApp
Left = 0
Top = 0
Caption = 'FrmApp'
ClientHeight = 245
ClientWidth = 516
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 13
object btnJSON: TButton
Left = 24
Top = 30
Width = 75
Height = 25
Caption = 'JSON'
TabOrder = 0
OnClick = btnJSONClick
end
object btnDataSet: TButton
Left = 24
Top = 96
Width = 75
Height = 25
Caption = 'DataSet'
TabOrder = 1
OnClick = btnDataSetClick
end
object memo: TMemo
Left = 176
Top = 8
Width = 321
Height = 229
TabOrder = 2
end
end

data.xml

<?xml version="1.0" standalone="yes"?>
<DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD attrname="VendorNo" fieldtype="r8"/>
<FIELD attrname="VendorName" fieldtype="string" WIDTH="30"/>
<FIELD attrname="City" fieldtype="string" WIDTH="20"/>
<FIELD attrname="State" fieldtype="string" WIDTH="20"/>
</FIELDS>
<PARAMS DEFAULT_ORDER="1" PRIMARY_KEY="1" LCID="1033"/>
</METADATA>
<ROWDATA>
<ROW VendorNo="2014" VendorName="Cacor Corporation" City="Southfield" State="OH"/>
<ROW VendorNo="2641" VendorName="Underwater" City="Indianapolis" State="IN" />
<ROW VendorNo="2674" VendorName="J.W. Luscher Mfg." City="Berkely" State="MA"/>
<ROW VendorNo="3511" VendorName="Scuba Professionals" City="Rancho Dominguez"/>
<ROW VendorNo="3819" VendorName="Divers&apos; Supply Shop" City="Macon" State="GA"/>
</ROWDATA>
</DATAPACKET>

最佳答案

why no memory leak occurs when using the JSON library.

If GetValue returns a TJSONValue that does not implement an interface. Why does my "if" not raise a memory leak?

TJSONObject.GetValue() 返回一个指向 TJSONValue 对象的指针,该对象由 TJSONObject 内部拥有。当您在过程结束时释放 TJSONObject 时,TJSONValue 也将被释放。 GetValue() 不会为输出分配任何新内存,因此没有任何内容可供您释放,因此不会泄漏。

why access violation does not occur when accessing the fields of the Cds.

If the Cds that provided the Data has been released from memory. Why does not access violation occur?

您将源 ClientDataSet 的 Data 属性分配给 OleVariant,然后将其分配给其他 ClientDataSet 的 Data 属性。 OleVariant 会复制分配给它的任何数据(对于接口(interface)对象的[数组],它会增加它们的引用计数),因此无论原始 ClientDataSet释放与否没有什么区别,因为 OleVariant 是独立的并管理它所持有的数据。

关于delphi - 为什么没有发生MemoryLeak或AccessViolation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57084819/

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