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

标签 delphi memory-leaks access-violation

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

首先是为什么使用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/

相关文章:

c - 此 C 代码中是否存在内存泄漏?

string - Delphi 应用程序泄漏 AnsiStrings

delphi - 有什么好的做法,如果在循环TStringList项时避免越界索引错误?

delphi - Canvas 绘图 - 如何改进这个 Alpha 绘图例程?

匹配运算符的 Perl 内存泄漏

c++ - 使用静态 std::vector 类成员时发生访问冲突

delphi - 如何使用 TIdHTTPServer 和 OpenSSL 在同一端口上支持 SSL 和非 SSL 流量?

objective-c - xcode 中泄漏仪器的准确性

c++ - 在 C++ 中捕获内存访问冲突

c - 文件解析期间访问违规写入位置