Link to home
Start Free TrialLog in
Avatar of hutelihut
hutelihut

asked on

Memory leak (dynamically created components)

I am using the tWave component.
tWave is used for editing and writing sound files (.wav)

The latest version of the tWave component can be found at http://home.bip.net/baxtrom/delphi/wave.zip

The following code loads c:\hugefile.wav into memory, and that occupies 20 megabytes of memory in my case. The problem is, that the line "w.free" does not free the memory used. The memory is released when the application terminates. How can I free the memory immediately?


procedure TForm1.Button1Click(Sender: TObject);
var
  w:tWave;
begin
  w:=tWave.Create(nil);
  try
    w.FileName:='C:\HugeFile.wav'; //This file is 20 megabytes on my computer.

    //w.filename is a property. Every time it is changed the file is loaded into memory


  finally
    w.free;
  end;
end;


I wonder why tWave has no Constructor or Destructor...
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hutelihut
hutelihut

ASKER

I just added the following lines to the component:

constructor TWave.Create(AOwner: TComponent);
begin
  FWaveStream:=nil;
  FTempWaveStream:=nil;
  Inherited Create(AOwner);
end;

destructor TWave.Destroy;
begin
  if Assigned(FWaveStream) then FWaveStream.Free;
  if Assigned(FTempWaveStream) then FTempWaveStream.free;

  FWaveStream:=nil;
  FTempWaveStream:=nil;
  Inherited Destroy;
end;

....it seems to work. I have just sent an email to the creator, and I am waiting for reply...
 if Assigned(FWaveStream) then FWaveStream.Free;
  if Assigned(FTempWaveStream) then FTempWaveStream.free;

  FWaveStream:=nil;
  FTempWaveStream:=nil;

you can do it simpler:
FreeAndNil(FWaveStream);
FreeAndNil(FTempWaveStream);

ziolko.
On thing I have seen that may help.... When I dynamically create dataset components in Delphi 2007 and pass Nil as the owner I get memory leaks.  If I pass in a form or data module then I get no leaks.  Try giving it an owner and see if it helps.