a Free procedure call doesn't free (and nil) an object

Posted on 2003-10-22
Last Modified: 2010-04-05
hi everyone,

i have the following problem :
i've created a TTimer descendent the following way :


  TMyTimer = Class(TTimer)
                    ... various variables and TList Objects...
                    constructor Create(aowner : TComponent);override;
                    destructor destroy; override;

var MyTimer : TMyTimer;

constructor TPopUpCampagne.Create(AOwner : Tcomponent);
     inherited Create(AOwner);
     ... several initializations including OnTimer affectation ...

destructor TPopUpCampagne.destroy;
     ... stuff to free memory...

     inherited destroy;

then, further in the source code, i do the following (i want to destroy MyTimer to re-create it - instead of updating its data) :

if Assigned(Mytimer) then;



the problem is that after the free call, the variable MyTimer isn't Nil - as expected ! but is still pointing to an existing object (and then my program throws an exception on another operation).

i've run the debugger and all the free calls in the object hierarchy are done.

Does anyone have an idea about what's happening ? and does anyone have a workaround ?
i could recreate the variable with the MyTimer:=TMyTimer.Create(Nil); but then i would have a memory leak...

thanks in advance,


PS : i put 150 points, cause it's important and i need a quick answer...
Question by:fsurfer
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
  • 4
LVL 27

Accepted Solution

kretzschmar earned 150 total points
ID: 9597806
instead of

if Assigned(Mytimer) then;


if Assigned(Mytimer) then

meikl ;-)
LVL 27

Expert Comment

ID: 9597824
just for eplanation

>the problem is that after the free call, the variable MyTimer isn't
>Nil - as expected !

the referenceVariable is never set to nil after calling the free-method,
thats a normal bahaviour, you as developer have to care about this

meikl ;-)
LVL 27

Expert Comment

ID: 9597853
btw. your timer is freed

Author Comment

ID: 9598092
Thanks for the answer !

it works perfectly !

i've always believe that free was doing a FreeAnd Nil....hmmmm there're always things to learn :)

LVL 27

Expert Comment

ID: 9598159
glad to helped you ;-)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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