Solved

Free bug?

Posted on 2002-04-04
3
136 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
Comment Utility
a safer alternative would be to use

FreeAndNil
0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 100 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now