Delphi - structures' strings not being freed [FastMM manager]

﹥>﹥吖頭↗ 提交于 2020-03-01 06:41:52

问题


If I declare

PSomeStruct = ^TSomeStruct;
TSomeStruct = record 
  s1 : string;
end;

and I run the following code:

var
  p: PSomeStruct;
begin
  new(p);
  p^.s1:= 'something bla bla bla';
  dispose(p);

the FastMM 4 memory manager reports that there was a memory leak (type: string, data dump: "something bla bla bla"). However, if I do set the s1 string to empty before calling dispose it's OK.

The second way I found is to change from record type to class, then instead of new I'm creating the instance, and instead of dispose I'm calling instance.Free(). It works without manually cleaning the strings.

Is there a way to make Delphi automatically clean my strings when I call dispose?


回答1:


Is FastMM the first unit used in your .dpr? Otherwise it could be finalized too early, reporting false memoryleaks.

And does this simplified codesample also generate the same memoryleak as when you use your JvSimpleXML? When it's not, there is probably more going on then you suspect.

In my opinion: when FastMM reports a memory leak, there is a memoryleak.




回答2:


You are already doing the correct thing. If FastMM says that string has leaked, then FastMM is wrong, or it's reporting a different string from the one you think it is. The Dispose procedure releases strings from records.

In this particular case, there shouldn't have been any memory allocated for that string anyway. It's a string literal, so I'd expect the compiler to assign that literal; its reference count should be -1 and FastMM never should have seen it.



来源:https://stackoverflow.com/questions/1202544/delphi-structures-strings-not-being-freed-fastmm-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!