Memory Leak

I am using Delphi 2007 on Vista.  I have the following defined:

TSettings = Class(TObject)
  Field1: Integer;
  Field2: Word
 ...
end;

var
MySettings: TSettings

On FormCreate:  

try
  MySettings := TSettings.Create
except
  MySettings := NIL;

On FormClose
  if MySettings<>NIL then MySettings.Free

Everything works perfectly; however, ReportMemoryLeaksOnShutdown reports an unexpected memory leak with this code.  I have tracked it down to the above but it escapes me as to how this is creating a memory leak.

Can anyone help?
alexiatAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mikelittlewoodConnect With a Mentor Commented:
Try changing

if MySettings<>NIL then MySettings.Free

to

if assigned( MySettings) then
try
   MySettings.Free
finally
   MySettings := nil
end;

This is how I usually do it
0
 
MerijnBSr. Software EngineerCommented:
does the constructor except, on other words, do you reach the line: MySettings := NIL; ?

btw, you _never_ have to check for NIL before you free, free does that for you itself:

procedure TObject.Free;
begin
  if Self <> nil then
    Destroy;
end;
0
 
alexiatAuthor Commented:
Thanks for the insight about checking for NIL.  

Regarding reaching the except clause, no, it doesn't.  The object is created just fine.  I commented out all code so that the only thing that executes is creating the object on FormCreate and then free'ing the object on FormClose.  If I comment out those lines of code I get no memory leak.  If I let them execute I do.
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
diniludCommented:
Any other class is there within the TSettings?
0
 
MerijnBSr. Software EngineerCommented:
in this case something in TSettings seems to be leeking, can you show some more code?
0
 
alexiatAuthor Commented:
I can't show more code until Monday; however, this form is in a package which is loaded in at run-time via loadpackage (and then unloadpackage). Could this cause something in TSettings to leak?
0
 
alexiatAuthor Commented:
I went into work today to try and figure this out.  When I tried what you suggested it fixed it.  I'm not sure why but I'm sure happy!  Thanks.
0
 
MerijnBSr. Software EngineerCommented:
very strange that it solved your problem, there is no actual difference in code.
0
 
alexiatAuthor Commented:
Extremely strange.  However, I have stopped even thinking things work strangely on Vista.  Seems to be the norm...
0
 
mikelittlewoodCommented:
Its because you still have the actual pointer hanging around even though it is pointing to something that doesnt actually exist anymore.
0
 
MerijnBSr. Software EngineerCommented:
nope, if the pointer points to nil or to somewhere in memory where there was something once, that does not make a difference at all.

The pointer uses 4 bytes, does not matter what's in the 4 bytes (where the pointer points to).
0
All Courses

From novice to tech pro — start learning today.