• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

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...
0
hutelihut
Asked:
hutelihut
  • 2
1 Solution
 
ziolkoCommented:
probably it's poorly written component, what you can do about it? well alter original code (if you have source files) otherwise contact creator of component.

if you decide to alter code yourself try FastMM (http://sourceforge.net/projects/fastmm/) which can help you a lot with finding memory leaks.

ziolko.
0
 
hutelihutAuthor Commented:
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...
0
 
ziolkoCommented:
 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.
0
 
developmentguruPresidentCommented:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now