I wrote a paragraph, at the address below, Delphi’s big bug, you don’t have to bother to read it, I will repeat it here.
But I think you can take a look at the comments below.
http://www.csdn.net/Develop/read_article.asp?id=12525
The original code is like this,
PRocedure TForm1.Button1Click(Sender: TObject);
var
strings :TStringList;
begin
if not Assigned(strings) then
begin
strings :=TStringList.Create;
// ShowMessage('sfdasfd'); If you add this or that sentence, there will be no problem, otherwise, haha.
end;
strings.free;
strings := nil;
end;
In fact, there is only one line that I want to focus on, and that is the strings.Free; line.
But there happens to be other codes that cause a lot of unnecessary trouble. I have emphasized it several times, but the core code is still covered by other garlands. And it always causes misunderstandings, so
I rewrite the code as follows:
procedure TForm1.Button1Click(Sender: TObject);
var
strings :TStringList;
begin
strings.free;
end;
In fact, the TStringList class in this code can be replaced with any class you want to try.
This way, if you use the CPU watch window you can find things, but if you don't use it, you just press Ctrl+F2
Terminate the program.
I don’t know if this title also means something stupid, but it’s in Chinese anyway. You and I both understand.
In fact, the purpose of this code is to remind you to pay attention to the correct use of objects. As for the reason for the error in this code,
I think it is indeed an omission of the compiler, but if you have no research on the memory structure of classes and objects, I suggest you: first, let it go, and second, study it.