无效的指针操作,通过调试请求的建议

Xan*_*nyx 2 memory delphi

我似乎已经创建了破坏内存的代码.

从来没有遇到过这样的问题,我现在设置了一个无效的指针操作.

在下面,我调用PromptForXYZPropertiesSettings后,const字符串sFilename的值被删除.

// Allow the user to quickly display the properties of XYZ without needing to display the full Editor
function PromptForXYZProperties(const sFilename:string; var AXYZProperties: TXYZProperties): boolean;
var
  PropEditor: TdlgEditor;
begin
  PropEditor:= TdlgEditor.create(nil);
  try
    PropEditor.LoadFromFile(sFilename);                   <-- sFilename = 'C:\My Folder\Some Folder.txt'
    PropEditor.SelectedXYZProperties := AXYZProperties;

    // Bypass PropEditor to show form owned by it
    Result := PropEditor.PromptForXYZPropertiesSettings;  

    if Result then
    begin
      PropEditor.SaveToFile(sFilename);                   <-- sFilename now somethign like 'B'#1#0#0'ë' or value of a different var
    end;
  finally
    PropEditor.free;      
  end;
end;
Run Code Online (Sandbox Code Playgroud)

其他详情:

  • Delphi 2007,Windows 7 64位,但在XP上测试EXE时可以重现
  • 从展览中删除CONST STOPS问题(但可能因此问题只是潜伏)
  • PropEditor.PromptForXYZPropertiesSettings创建并显示表单.如果我禁用ShowModal调用,则内存不会被删除.即使我从表单中删除了所有控件和代码

所以我想就如何调试问题提出一些建议.我想也许正在观察sFilename var存在的内存指针,看看它在哪里被删除,但不知道我会怎么做(显然需要在应用程序中完成所以拥有内存).

谢谢

Mas*_*ler 6

听起来像是在捣乱你的筹码.从随便看看你的代码我看不出任何明显的正确性问题.您有正确的想法:要跟踪此情况,您需要监控字符串的值并查看其更改时间.这是你如何做到这一点:

  • 在方法的第一行放置一个断点.
  • 当它中断时,请查看调试器中的Local Variables视图.找到sFilename并双击它.
  • Debug Inspector窗口将打开.在顶部它会说这样的事情:sFileame: string $18FEA8 : $4A0E5C.这两个十六进制值分别是对字符串的引用和字符串数据本身的位置.
  • 按CTRL-ALT-B调出断点列表.它有一个小工具栏,工具栏上的第一个按钮有一个下拉箭头.
  • 单击此箭头,然后从列表中选择"数据断点".您将需要为Debug Inspector窗口中的两个值创建两个断点.(如果它警告你在堆栈上放置一个数据断点,那么无论如何都要这样做,但是你要记得事后清除它.)
  • 一旦设置了两个数据断点值,点击F9.系统将监视这些内存位置,并在修改后中断.从那里你应该能够找到腐蚀你的弦的东西.