Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-10-22
5
Medium Priority
?
237 Views
Last Modified: 2010-04-05
hi everyone,

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

interface

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

var MyTimer : TMyTimer;

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

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

     inherited destroy;
End;

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
   MyTimer.free;

...

MyTimer:=TMyTimer.Create(Nil);

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,

Guillaume

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

Accepted Solution

by:
kretzschmar earned 600 total points
ID: 9597806
instead of

if Assigned(Mytimer) then
   MyTimer.free;

use

if Assigned(Mytimer) then
   FreeAndNil(MyTimer);

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
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 ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9597853
btw. your timer is freed
0
 

Author Comment

by:fsurfer
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 :)

Guillaume
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9598159
glad to helped you ;-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 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