Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory Leak Help

Status
Not open for further replies.

tjcusick

Programmer
Dec 26, 2006
134
US
I am getting a memory leak and my EurekaLog is telling me that is it starting at the follow code. This following code is used to setup a Dymo Twin Label Printer.

Code:
procedure DoPrepDymo;
begin    //Create Dymo Label ActiveX objects.
  try
    DL := CreateOleObject(IDymoAddInName) as IDymoAddIn5;
    DL2 := CreateOleObject(IDymoAddInName) as IDymoAddIn5;
    LB := CreateOleObject(IDymoLabelsName) as IDymoLabels;
  except
    MessageDlg ('Unable to create Dymo Label ActiveX objects.', mtError, [mbOk], 0)
  end;
  // get the label files directory from Registry
  with TRegistry.Create do begin
    RootKey := HKEY_CURRENT_USER;
    if OpenKey(sDirectoriesKey,FALSE) then begin
      LabelDirName := ReadString(sLabelDirName);
      CloseKey;
    end
    else LabelDirName := '';
  end;
  DL.Open(ExecDir+'SPCRReceipt2.LWL');
end;

So i'm guessing that since i'm creating them that i need to free them. Unfortunately there is no documentation (that i have been able to find) that tells me how to free them up. I've looked and found some stuff on "CreateOldObject" when opening applications like excel and word and all they do is Free and Unassigned the object but that wont work here ... (I know cus i tried it).

Does anyone have any suggestions as to how I free up this memory leak?
 
well, the "with TRegistry.Create" statement is what I call "looking for problems".

always assign your objects to variables so that you can clean them up afterwards.

like:

Code:
procedure DoPrepDymo;

var Reg : TRegistry;

begin    //Create Dymo Label ActiveX objects.
  try
    DL := CreateOleObject(IDymoAddInName) as IDymoAddIn5;
    DL2 := CreateOleObject(IDymoAddInName) as IDymoAddIn5;
    LB := CreateOleObject(IDymoLabelsName) as IDymoLabels;
  except
    MessageDlg ('Unable to create Dymo Label ActiveX objects.', mtError, [mbOk], 0)
  end;
  // get the label files directory from Registry
  Reg := TRegistry.Create 
  try
   with Reg do begin;
    RootKey := HKEY_CURRENT_USER;
    if OpenKey(sDirectoriesKey,FALSE) then begin
      LabelDirName := ReadString(sLabelDirName);
      CloseKey;
    end
    else LabelDirName := '';
  end;
  finally
   FreeAndNil(Reg);
  end;
  DL.Open(ExecDir+'SPCRReceipt2.LWL');
end;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Great thanks daddy!!! That got rid of that memory leak..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top