procedure IAmCalled*Very*FrequentlyToDoDBOperations;

procedure IAmCalled*Very*FrequentlyToDoDBOperations;
var
  ms: TMemoryStream;
  m1: TMemoryStream;
  tmp: TMemoryStream;
  ...
begin
  ms := TMemoryStream.Create;
  m1 := TMemoryStream.Create;
  tmp := TMemoryStream.Create;
  try
    // about a dozen lines of code that operate on streams
  finally
    ms := nil;
    tmp := nil;
    m1 := nil;
  end;
  // some condition
  tmp.free;
end;

after running the app for about 3-5min. the app would have leaked around 2MB... more or less depending on what the user would have done with the app, but in a typical usage, it would have leaked thens of MB and in some corner cases, some streams would have been "freed" after "nil-ing".

Comments

  1. nil-ing your variable is not correct ! You must call Free.

    ReplyDelete
  2. This is what happens when you first lern language with garbage collection and then try to program in real language ;)

    ReplyDelete
  3. Maciej Bąk you hit the nail right there, the code style indicated that he is a java dev mainly.

    ReplyDelete
  4. Real devs come from asm or C, the rest are plushies ;-)

    ReplyDelete
  5. Alexey Petushkov most likely java because the project is full of interfaces and every "get" and "set" method begins with "get" and "set"

    ReplyDelete
  6. Eric Grange Real programmers dont worry about language they programming on. ;)

    ReplyDelete
  7. I started with Z80 asm before moving to 68K asm and finally x86 asm :)
    I am used to cleaning up my own mess.

    ReplyDelete
  8. I started well before Z80! The first processor I programmed was a National Semiconductor SC/MP, alias SCAMP (http://en.wikipedia.org/wiki/National_Semiconductor_SC/MP). I used it in 1978. It ran at 1 MHz. There was no OS at all. Later I switched to the Intel 8080, then NSC800, the Z80, then Intel 8086. I started C language with Z80 but still done a lot of assembler for performance reasons. I also used a Motorola 68000 programmed in Pascal ! Then came the PC. You know where we are today :-)

    ReplyDelete
  9. Well, I started on Z80, first basic then asm (cpc 464, did some 8bit demo scene, when you had to sync with the CRT to squeeze extra colors, and when non-synthesized audio was done by hacking the buzzer with PWM, yes, the buzzer, not the fancy DACs that came later on). Also did some Z80 on specialized industrial hardware later on. Then mostly C/C++ across various hardwares and compilers, before finding Delphi 2.

    ReplyDelete
  10. For bonus sad points, replace those with TFileStreams. Yup, seen that... Now your frequent DB operations are even further slowed down by unnecessary I/O.

    ReplyDelete
  11. begin
      ms := TMemoryStream.Create;
      m1 := TMemoryStream.Create;
      tmp := TMemoryStream.Create;
      try
        // about a dozen lines of code that operate on streams
      finally
        ms.free;
        tmp.free;
        m1.free;
      end;
      // some condition
      // tmp.free; should not free anymore.
    end;

    ReplyDelete

Post a Comment