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.w av'; //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...
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
var
w:tWave;
begin
w:=tWave.Create(nil);
try
w.FileName:='C:\HugeFile.w
//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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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...