如何在Delphi中泄漏一个字符串 [英] How to leak a string in Delphi

查看:129
本文介绍了如何在Delphi中泄漏一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和一个同事聊天,如果你真的搞砸了,你可以如何在Delphi中泄漏一个字符串。默认情况下,字符串被引用计数并自动分配,所以它们通常只是工作没有任何想法 - 不需要手动分配,大小计算或内存管理。

I was talking to a co-worker the other day about how you can leak a string in Delphi if you really mess things up. By default strings are reference counted and automatically allocated, so they typically just work without any thought - no need for manual allocation, size calculations, or memory management.

但我记得读过一次,直接有一种方法来泄露一个字符串(不包含在被泄露的对象中)。它似乎是通过引用传递一个字符串,然后从更大的范围内从传递到它的例程中访问它。是的,我知道这是模糊的,这就是为什么我在问这个问题。

But I remember reading once that there is a way to leak a string directly (without including it in an object that gets leaked). It seems like it had something to do with passing a string by reference and then accessing it from a larger scope from within the routine it was passed to. Yeah, I know that is vague, which is why I am asking the question here.

推荐答案

实际上,传递字符串作为CONST或非const在Delphi 2007和2009中的引用计数相同。当字符串作为CONST传递时导致访问冲突的情况。这是一个问题一个

Actually, passing string as CONST or non const are the same in term of reference count in Delphi 2007 and 2009. There was a case that causing access violation when string is passed as CONST. Here is the problem one

type
  TFoo = class
    S: string;
    procedure Foo(const S1: string);
  end;

procedure TFoo.Foo(const S1: string);
begin
  S:= S1; //access violation
end;

var
  F: TFoo;
begin
  F:= TFoo.create;
  try
    F.S := 'S';
    F.Foo(F.S);
  finally
    F.Free;
  end;
end.

这篇关于如何在Delphi中泄漏一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆