Solved

Free bug?

Posted on 2002-04-04
3
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

631 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