Solved

Free bug?

Posted on 2002-04-04
3
139 Views
Last Modified: 2010-04-04
I had this very strange bug the last day. It's probally easiest if I show you the bug in code.
I have a radiobutton on a form.

formcreate
var
  r: tregistry;
begin
  r.free; //if I don't comment this out rad is destroyed and is nil
  if radiobutton.checked then ...
 
end;
 

is this a bug in delphi ? I thought that a call to free would always be safe even when it's wrong.
0
Comment
Question by:bnz
3 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6920210
a safer alternative would be to use

FreeAndNil
0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 100 total points
ID: 6920232
This is wrong code.
A local variable resides on the stack and is NOT initialized to nil.
r.Free only checks for nil and does not call the destructor then. This is a bit of compiler magic.
Since r contains a random value the destructor is called and tries to deallocate data which has never been allocated.
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6922392
Well I suspect you already now this but before an object can be freed it has to exist first. In this case the object was never created so it can't be freed.

r := TRegistry.Create;


The Crazy One
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question