Solved

Free bug?

Posted on 2002-04-04
3
137 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

18 Experts available now in Live!

Get 1:1 Help Now