cnpack / cnvcl

CnPack VCL Components
http://www.cnpack.org
341 stars 103 forks source link

CnTwain Memory Leak #1

Closed nonameplum closed 9 years ago

nonameplum commented 9 years ago

Hi, I found memory leaks in CnTwain.pas.

In methods NativeTransfer, MemoryTransfer and FileTransfer, hbm_acq is not freed after use and causes fast growing memory leak.

I made changed in this places: NativeTransfer:

...
      TWRC_XFERDONE:
        begin
          hbm_acq := hBitMap;
          twRC2 := lpDSM_Entry(@FappID, @FdsID, DG_CONTROL, DAT_PENDINGXFERS, MSG_ENDXFER, @twPendingXfer);
          if twRC2 <> TWRC_SUCCESS then
            DoTwMessage('DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER', False);
          if twPendingXfer.Count = 0 then
            if (hbm_acq <> 0) and (GlobalLock(hbm_acq) <> nil) then
            begin
              Terminate(True);
              GlobalUnlock(hbm_acq);
            end;
          if hbm_acq > VALID_HANDLE then begin
            DoXferDone(hbm_acq);
            GlobalFree(hbm_acq);
          end else
            DoXferDone(0);
...

MemoryTransfer:

...
                TWRC_XFERDONE:
                  begin
                    GlobalUnlock(hbm_acq);
                    FlipBitMap(FHandle, hbm_acq, info.PixelType);
                    twRC2 := lpDSM_Entry(@FappID, @FdsID, DG_CONTROL, DAT_PENDINGXFERS, MSG_ENDXFER, @twPendingXfer);
                    if twRC2 <> TWRC_SUCCESS then
                      DoTwMessage('DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER', False);
                    if twPendingXfer.Count = 0 then
                      Terminate(True);
                    DoXferDone(hbm_acq);
                    GlobalFree(hbm_acq);
                  end;
...

FileTransfer:

...
              hbm_acq := GlobalAlloc(GHND, header.bfSize);
              if hbm_acq <> 0 then
              begin
                ptr := GlobalLock(hbm_acq);
               //for count:=(header.bfSize-sizeof(BITMAPFILEHEADER)) downto count; count-=num, ptr+=num)
                count := header.bfSize - sizeof(BITMAPFILEHEADER);
                while count > 0 do
                begin
                  if count < num then
                    num := count;
                  _lread(hF, ptr, num);
                  Dec(count, num);
                  Inc(ptr, num);
                end;
                GlobalUnlock(hbm_acq);
              end;
              GlobalFree(hbm_acq);
              _lclose(hF);
...
shanzhashu commented 9 years ago

Thanks. We'll correct it.

shanzhashu commented 9 years ago

OK. I remove an unused GlobalFree at line 981.