TAwImageGrid - Program leaks memory if the last image is deleted - delphi

Using: Delphi XE7 Update 1, TAwImageGrid, Windows 10 Professional running on Intel Core i7-2820QM.
This code loads images into the grid from a database:
var
s, w: String;
r: Integer;
ms: TMemoryStream;
bmp: TBitmap;
begin
r := uqProj_Search.RecordCount;
// Load images
for r := imgGrid.Count - 1 downto 0 do
imgGrid.Items.Images[r].Free;
imgGrid.Clear;
ms := TMemoryStream.Create;
try
while not(uqProj_Search.Eof) do
begin
r := uqProj_Search.FieldByName('row_id').AsInteger;
// :proj_id
uqImg_S.ParamByName('proj_id').AsInteger := r;
uqImg_S.Prepared := True;
uqImg_S.Open;
ms.Clear;
uqImg_Simg.SaveToStream(ms);
uqImg_S.Close;
ms.Position := 0;
bmp := TBitmap.Create;
try
bmp.LoadFromStream(ms);
imgGrid.Items.Add(IntToStr(r));
imgGrid.Items.Images[imgGrid.Count - 1] := TBitmap.Create;
imgGrid.Items.Images[imgGrid.Count - 1].Assign(bmp);
finally
bmp.Free;
end;
uqProj_Search.Next;
end;
finally
ms.Free;
end;
end;
I have this code in the KeyDown event (called when the Del key is pressed):
procedure TfmSrchRec.imgGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
x, p: Integer;
AFormat: Word;
AData: THandle;
APalette: HPalette;
begin
x := imgGrid.ItemIndex;
p := StrToInt(imgGrid.Items.Strings[x]);
if (x = -1) then
Exit;
if (UpCase(Char(Key)) = 'C') and (Shift = [ssCtrl]) then
begin
// Clipboard.Assign(imgGrid.Images[imgGrid.ItemIndex])
TBitmap(imgGrid.Images[x]).Dormant;
TBitmap(imgGrid.Images[x]).SaveToClipboardFormat(AFormat, AData, APalette);
Clipboard.SetAsHandle(AFormat, AData);
end
else if (Key = VK_DELETE) then
begin
imgGrid.Items.Images[x].Free;
imgGrid.Items.Delete(x);
end;
end;
Freeing up memory in the form's OnClose event:
procedure TfmSrchRec.FormClose(Sender: TObject; var Action: TCloseAction);
var
r: Integer;
begin
for r := imgGrid.Count - 1 downto 0 do
imgGrid.Items.Images[r].Free;
end;
Here's the problem:
After deleting an image from the grid, if that image was the last remaining image, then closing the program would produce this error message:
---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block leaks are:
61 - 68 bytes: Unknown x 1
---------------------------
OK
---------------------------
The error does not occur if there was a remaining image in the grid before the application was closed. I have ReportMemoryLeaksOnShutDown := True at project startup (in the DPR file).
I'm guessing that this error has to do with the component's code more than the way I am using it. I'm hoping that the TAwImageGrid component author NGLN could have a look at this question and provide the answer, but other Delphi gurus are also welcome.
Links:
TAwImageGrid component source official home page:
https://github.com/NGLN/AwImageGrid
StackOverflow Question that gives a good introduction to the component:
Looking for a custom image grid

I can reproduce your findings and consider it a bug.
When making the component, I copied the implementation of TStringList from D7, i.e. by using a pointer to a non-existing fix-sized array for the internal storage of the items. Strangely enough, I cannot find flaws in it, but D7's TStringList implementation does not produce this bug. I suppose it has something to do as explained here.
I see that the implementation of TStringList in XE2 is changed to the use of a dynamic array. When I change the component's code to that same design, the memory leak is gone. So I will change the open source code too, but for the time being you might do yourself.

Related

Delete Files With progressbar

I'm trying to make progressbar while deleting files here is my code:
procedure TForm1.Timer1Timer(Sender: TObject);
var
i:Integer;
begin
i:=i+1;
ProgressBar.Max:=DeleteList.Count - i ; //Files = 8192
DeleteFile(GetIniString('List', 'File' + IntToStr(i),'','FileLists.ini'));
ProgressBar.Position:=ProgressBar.Position+1;
end;
Using threads or IFileOperation both involve fairly steep learning curves. Here are a couple of possibilities:
TDirectory method
At Jerry Dodge's prompting I decided to add an example of using TDirectory to
get a list of files and process it in some way, e.g. delete files in the list.
It displays a periodic progress message - see the if i mod 100 = 0 then statement
in the ProcessFiles method. Unfortunately I couldn't find a way to show
a periodic message during the list-building stage because AFAIC TDirectory
doesn't expose the necessary hook to do so.
procedure TForm2.ProcessFileList(FileList : TStringList);
var
i : Integer;
S : String;
begin
for i := 0 to FileList.Count - 1 do begin
// do something with FileList[i], e.g. delete it
S := FileList[i];
DeleteFile(S);
// Display progress
if i mod 100 = 0 then // do something to show progress
Caption := Format('Files processed: %d ', [i]);
// OR, you could use i and FileList.Count to set a trackbar % complete
end;
Caption := Format('Processed: %d files', [FileList.Count]);
end;
procedure TForm2.GetFileList(const Path : String; FileList : TStringList);
var
Files : Types.TStringDynArray;
i : Integer;
begin
Files := TDirectory.GetFiles('C:\Temp');
FileList.BeginUpdate;
try
for i:= 0 to Length(Files) - 1 do
FileList.Add(Files[i]);
finally
FileList.EndUpdate;
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
FileList : TStringList;
begin
FileList := TStringList.Create;
try
GetFileList('C:\Temp', FileList);
ProcessFileList(FileList);
Memo1.Lines.Text := FileList.Text;
finally
FileList.Free;
end;
end;
It should be evident that this way of doing it is a lot simpler than using the
traditional, Windows-specific method below, at the expense of loss of some flexibility,
and has the advantage of being cross-platform.
IFileOperation method (Windows-specific)
The Windows API has functionality to retrieve and process a list of files e.g. in a directory and there used to be a trivially-simple-to-use wrapper around this, including a progress animation, in the (antique) v.3 of SysTools library from TurboPower S/Ware, but I'm not sure this wrapper ever made it into the later public domain version. On the face if it, it could also be done using the IFileOperation interface but google has yet to conjure a simple example. Note that an SO answer about this contains the comment "this is a very complex API and you do need to read the documentation carefully".
I attempted to do this myself but soon got out of my depth. Remy Lebeau's answer here to the q I posted when I got stuck shows how to do it, but the TDirectory method above seems vastly easier at my skill level.
Traditional (D7) method (Windows-specific)
In my experience, if you are only looking to process a few hundred thousand files, you should be able to do it, displaying progress as you go, by adding the files to a TStringList and then processing that, with code along the following lines:
procedure GetFileList(const Path : String; Recurse : Boolean; FileList : TStringList);
// Beware that the following code is Windows-specific
var
FileCount : Integer;
procedure GetFilesInner(sPath : String);
var
Path,
AFileName,
Ext: String;
Rec: TSearchRec;
Done: Boolean;
begin
Path := IncludeTrailingBackslash(sPath);
if FindFirst(Path + '*.*', faAnyFile, Rec) = 0 then begin
Done := False;
while not Done do begin
if (Rec.Name <> '.') and (Rec.Name <> '..') then begin
AFileName := Path + Rec.Name;
Ext := LowerCase(ExtractFileExt(AFileName));
if not ((Rec.Attr and faDirectory) = faDirectory) then begin
inc(FileCount);
if FileCount mod 100 = 0 then
//show progress in GUI
;
FileList.Add(AFileName)
end
else begin
if Recurse then
GetFilesInner(AFileName);
end;
end;
Done := FindNext(Rec) <> 0;
end;
FindClose(Rec);
end;
end;
begin
FileCount := 0;
FileList.BeginUpdate;
FileList.Sorted := True;
FileList.Duplicates := dupIgnore; // don't add duplicate filenames to the list
GetFilesInner(Path);
FileList.EndUpdate;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
FileList : TStringList;
FileName : String;
i : Integer;
begin
FileList := TStringList.Create;
try
GetFileList('d:\aaad7', True, FileList);
for i := 0 to FileList.Count - 1 do begin
FileName := FileList[i];
// do something with FileName, e.g. delete the file
if i mod 100 = 0 then
// display progess e.g. by
Caption := IntToStr(i);
end;
Memo1.Lines := FileList;
finally
FileList.Free;
end;
end;
The if [...] mod [...] = 0 then statements are where you can show the two phases' progress howver you want.
Btw, this code was olny intended to get you started. I'm obliged to Jerry Dodge for reminding me that in recent versions of Delphi, there is similar functionality built-in, by way of the TDirectory.GetFiles method so if you are interested in cross-platform and/or accommodate Unicode, you would do better to study the ins and outs of TDirectory and non-Windows-specific routines like TrailingPathDelim.
When you really want to show some progress in a UI when deleting files, you should use threads:
create a thread, which deletes the files
then poll the progress of the deletion thread from the UI
Be careful when using threads, not to access UI parts (like the progressbar) from within the deletion thread. Such things should at least be synchronized.

Delphi 64bit DLL: OleCtrls events issues

I've converted a DLL from 32 bit to 64 bit having no problem, but when I load this DLL from a 64 bit application that occupies a large amount of memory the application crashes and closes itself when DLL is loaded.
The DLL is a simple form with a TWebBrowser on it. I use Delphi 10 Seattle.
After debugging I found a 64 bit conversion problem in the vcl unit "Vcl.OleCtrls.pas" solved in this way:
procedure TOleControl.HookControlWndProc;
var
WndHandle: HWnd;
begin
if (FOleInPlaceObject <> nil) and (WindowHandle = 0) then
begin
WndHandle := 0;
FOleInPlaceObject.GetWindow(WndHandle);
if WndHandle = 0 then raise EOleError.CreateRes(#SNoWindowHandle);
WindowHandle := WndHandle;
//DefWndProc := Pointer(GetWindowLong(WindowHandle, GWL_WNDPROC));//OLD
DefWndProc := Pointer(GetWindowLongPtr(WindowHandle, GWL_WNDPROC));
CreationControl := Self;
//SetWindowLong(WindowHandle, GWL_WNDPROC, Longint(#InitWndProc));//OLD
SetWindowLongPtr(WindowHandle, GWL_WNDPROC, LONG_PTR(#InitWndProc));
SendMessage(WindowHandle, WM_NULL, 0, 0);
end;
end;
This solves the crash issue, but TWebBrowser events are not fired anymore and happens on 64bit only.
How can I fix TWebBrowser events firig?
Have you find similar issue or workaroud to fix events?
Thanks
I found another cast error that generate the TWebBrowser event issue.
In Emba unit "Vcl.OleCtrls.pas":
procedure TOleControl.InvokeEvent(DispID: TDispID; var Params: TDispParams);
{$IFDEF CPUX64}
var
EventMethod: TMethod;
ParamBlock : TParamBlock;
i : Integer;
StackParams2 : array of Int64;
begin
GetEventMethod(DispID, EventMethod);
//if Integer(EventMethod.Code) < $10000 then Exit; //OLD
if Int64(EventMethod.Code) < $10000 then Exit; //NEW
ParamBlock.RegRCX := Int64(EventMethod.Data);
ParamBlock.RegRDX := Int64(Self);
if Params.cArgs > 2 then
begin
SetLength(StackParams2, Params.cArgs-2);
end;
for i := 1 to Params.cArgs do
case i of
1: ParamBlock.RegR8 := Int64(Params.rgvarg[Params.cArgs-1].unkVal);
2: ParamBlock.RegR9 := Int64(Params.rgvarg[Params.cArgs-2].unkVal);
else
StackParams2[i-3] := Int64(Params.rgvarg[Params.cArgs-i].unkVal);
end;
ParamBlock.StackDataSize := Length(StackParams2) * sizeof(Pointer);
ParamBlock.StackData := #StackParams2[0];
RawInvoke(EventMethod.Code, #ParamBlock);
end;
{$ELSE !CPUX64}
Integer cast generate an overflow, on hight memory usage situation, and the InvokeEvent procedure exit not calling the actual event. Solved with Int64 cast.
I hope Emba will integrate this fix and find similar.

Does this code fail to build because of a compiler bug?

Building, not just compiling, the following fails with an internal compiler error when using Delphi 6 if optimization is on. Using the assignment instead of the inc() works. Is this a compiler bug? The weird record structures are because the original code has been reduced to this minimal example.
program Project1;
type
requestCountsType = array[0..1] of
record
processed: int64;
end;
talliestype = record
counts: requestCountsType;
end;
healthtype = record
charged: talliestype;
end;
procedure computeProcessed(const h: healthtype; var requests, bytesin, bytesout: int64);
var i: byte;
begin
requests := 0; bytesin := 0; bytesout := 0;
for i := 0 to 1 do
begin
inc(requests, h.charged.counts[i].processed); // including this generates compiler internal error C1405 when optimization is on
// requests := requests + h.charged.counts[i].processed; // this works
end;
end;
var ht: healthtype; var r, b1, b2: int64;
begin
computeProcessed(ht, r, b1, b2);
end.
See bug report #109124. I can confirm the problem in Delphi XE; the bug report says it was fixed in Delphi XE4.

Is there a GetMouseMovePointsEx function in Lazarus?

In this other question I asked: Drawing on a paintbox - How to keep up with mouse movements without delay?.
The function GetMouseMovePointsEx was brought to my attention by Sebastian Z, however in Lazarus I am unable to find this function.
He mentioned that in Delphi XE6 it is in Winapi.Windows.pas, in Lazarus though it is not in Windows.pas.
I understand Lazarus is by no means an exact copy of Delphi but this function sounds like it could be the answer I am looking for in that other question. Im just having a hard time finding where it is and even getting any Delphi documentation on it. I do have Delphi XE but right now it is not installed and my project is been written in Lazarus.
I did a Find in Files... search from the Lazarus IDE targeting the install folder and the only result that came back was from one of the fpc sources in:
lazarus\fpc\2.6.4\source\packages\winunits-jedi\src\jwawinuser.pas
I am not sure if I should use the above unit or not, or whether Lazarus has a different variant to GetMouseMovePointsEx?
Does anyone using Lazarus have any experience with GetMouseMovePointsEx and if so where can I find it?
Thanks.
Here's a quick example using Delphi. What you still need to do is filter out the points you've already received.
type
TMouseMovePoints = array of TMouseMovePoint;
const
GMMP_USE_HIGH_RESOLUTION_POINTS = 2;
function GetMouseMovePointsEx(cbSize: UINT; var lppt: TMouseMovePoint; var lpptBuf: TMouseMovePoint; nBufPoints: Integer; resolution: DWORD): Integer; stdcall; external 'user32.dll';
function GetMessagePosAsTPoint: TPoint;
type
TMakePoints = packed record
case Integer of
1: (C : Cardinal);
2: (X : SmallInt; Y : SmallInt);
end;
var
Tmp : TMakePoints;
begin
Tmp.C := GetMessagePos;
Result.X := Tmp.X;
Result.Y := Tmp.Y;
end;
function GetMousePoints: TMouseMovePoints;
var
nVirtualWidth: Integer;
nVirtualHeight: Integer;
nVirtualLeft: Integer;
nVirtualTop: Integer;
cpt: Integer;
mp_in: MOUSEMOVEPOINT;
mp_out: array[0..63] of MOUSEMOVEPOINT;
mode: Integer;
Pt: TPoint;
I: Integer;
begin
Pt := GetMessagePosAsTPoint;
nVirtualWidth := GetSystemMetrics(SM_CXVIRTUALSCREEN) ;
nVirtualHeight := GetSystemMetrics(SM_CYVIRTUALSCREEN) ;
nVirtualLeft := GetSystemMetrics(SM_XVIRTUALSCREEN) ;
nVirtualTop := GetSystemMetrics(SM_YVIRTUALSCREEN) ;
cpt := 0 ;
mode := GMMP_USE_DISPLAY_POINTS ;
FillChar(mp_in, sizeof(mp_in), 0) ;
mp_in.x := pt.x and $0000FFFF ;//Ensure that this number will pass through.
mp_in.y := pt.y and $0000FFFF ;
mp_in.time := GetMessageTime;
cpt := GetMouseMovePointsEx(SizeOf(MOUSEMOVEPOINT), mp_in, mp_out[0], 64, mode) ;
for I := 0 to cpt - 1 do
begin
case mode of
GMMP_USE_DISPLAY_POINTS:
begin
if (mp_out[i].x > 32767) then
mp_out[i].x := mp_out[i].x - 65536;
if (mp_out[i].y > 32767) then
mp_out[i].y := mp_out[i].y - 65536;
end;
GMMP_USE_HIGH_RESOLUTION_POINTS:
begin
mp_out[i].x := ((mp_out[i].x * (nVirtualWidth - 1)) - (nVirtualLeft * 65536)) div nVirtualWidth;
mp_out[i].y := ((mp_out[i].y * (nVirtualHeight - 1)) - (nVirtualTop * 65536)) div nVirtualHeight;
end;
end;
end;
if cpt > 0 then
begin
SetLength(Result, cpt);
for I := 0 to cpt - 1 do
begin
Result[I] := mp_out[I];
end;
end
else
SetLength(Result, 0);
end;
// the following is for demonstration purposes only, it still needs some improvements like filtering out points that were already processed. But it's good enough for painting a blue line on a TImage
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
MMPoints: TMouseMovePoints;
Pt: TPoint;
I: Integer;
begin
Image1.Canvas.Pen.Color := clBlue;
MMPoints := GetMousePoints;
for I := 0 to Length(MMPoints) - 1 do
begin
Pt.x := MMPoints[I].x;
Pt.y := MMPoints[I].y;
Pt := Image1.ScreenToClient(Pt);
if I = 0 then
Image1.Canvas.MoveTo(PT.X, pt.y)
else
Image1.Canvas.LineTo(PT.X, pt.y);
end;
end;
This function is implemented as part of the Win32 library. It is no more a Delphi or FPC function than it is a C++ or VB function. You import it from Win32.
In Delphi, this importing is achieved by way of the declaration of the function in the Windows unit. If you examine the source of this unit you'll find lots of type and constant declarations, as well as functions. The functions are typically implemented using the external keyword which indicates that the implementation is external to this code. The Windows unit is what is known as a header translation. That is it is a translation of the C/C++ header files from the Win32 SDK.
So you need a header translation with this function. The JEDI header translations are the most usual choice. And it seems that you've already found them. If the versions supplied with FPC serve your needs, use them.
Sometimes you might find yourself on the bleeding edge of progress and need to use a function that has not been included in any of the standard header translations. In that scenario it's usually simple enough to perform the translation yourself.

How do you drag and drop a file from Explorer Shell into a VirtualTreeView control in a Delphi application?

There is extensive drag and drop support in VirtualTreeView by Mike Lischke, and I am using TVirtualStringTree, which has some on-drag-and-drop events, but I can not figure out how to get it to accept a shell drag-and-drop of some files from the windows explorer shell, into my application. I want to load the files, when they are dragged onto the drop control.
I tried using a third-party set of code from Anders Melander, to handle drag and drop, but because VirtualTreeView already registers itself as a drop target, I can't use that.
edit: I found a simple workaround: Turn off toAcceptOLEDrop in VT.TreeOptions.MiscOptions.
It would be cool if anybody knows a way to use VirtualTreeView without using a third party OLE-shell-drag-drop library and using its extensive OLE drag/drop support to extract a list of filenames dragged in from the Shell.
My implementation (Works very well with Delphi 2010. Must add ActiveX, ShellApi to uses to compile):
procedure TfMain.vstTreeDragDrop(Sender: TBaseVirtualTree; Source: TObject;
DataObject: IDataObject; Formats: TFormatArray; Shift: TShiftState;
Pt: TPoint; var Effect: Integer; Mode: TDropMode);
var
I, j: Integer;
MyList: TStringList;
AttachMode: TVTNodeAttachMode;
begin
if Mode = dmOnNode then
AttachMode := amInsertBefore
else if Mode = dmAbove then
AttachMode := amInsertBefore
else if Mode = dmBelow then
AttachMode := amInsertAfter
else
AttachMode := amAddChildLast;
MyList := TStringList.Create;
try
for i := 0 to High(formats) - 1 do
begin
if (Formats[i] = CF_HDROP) then
begin
GetFileListFromObj(DataObject, MyList);
//here we have all filenames
for j:=0 to MyList.Count - 1 do
begin
Sender.InsertNode(Sender.DropTargetNode, AttachMode);
end;
end;
end;
finally
MyList.Free;
end;
end;
procedure TfMain.GetFileListFromObj(const DataObj: IDataObject;
FileList: TStringList);
var
FmtEtc: TFormatEtc; // specifies required data format
Medium: TStgMedium; // storage medium containing file list
DroppedFileCount: Integer; // number of dropped files
I: Integer; // loops thru dropped files
FileNameLength: Integer; // length of a dropped file name
FileName: string; // name of a dropped file
begin
// Get required storage medium from data object
FmtEtc.cfFormat := CF_HDROP;
FmtEtc.ptd := nil;
FmtEtc.dwAspect := DVASPECT_CONTENT;
FmtEtc.lindex := -1;
FmtEtc.tymed := TYMED_HGLOBAL;
OleCheck(DataObj.GetData(FmtEtc, Medium));
try
try
// Get count of files dropped
DroppedFileCount := DragQueryFile(
Medium.hGlobal, $FFFFFFFF, nil, 0
);
// Get name of each file dropped and process it
for I := 0 to Pred(DroppedFileCount) do
begin
// get length of file name, then name itself
FileNameLength := DragQueryFile(Medium.hGlobal, I, nil, 0);
SetLength(FileName, FileNameLength);
DragQueryFileW(
Medium.hGlobal, I, PWideChar(FileName), FileNameLength + 1
);
// add file name to list
FileList.Append(FileName);
end;
finally
// Tidy up - release the drop handle
// don't use DropH again after this
DragFinish(Medium.hGlobal);
end;
finally
ReleaseStgMedium(Medium);
end;
end;
I use this method to capture (receive) files dragged into a TWinControl from explorer.
You can test it on your control. In a standard TTreeView work fine.
Add ShellAPI to uses.
At private section:
private
originalEditWindowProc : TWndMethod;
procedure EditWindowProc(var Msg:TMessage);
// accept the files dropped
procedure FilesDrop(var Msg: TWMDROPFILES);
At OnCreate of your form:
// Assign procedures
originalEditWindowProc := TreeView1.WindowProc;
TreeView1.WindowProc := EditWindowProc;
// Aceptar ficheros arrastrados // Accept the files
ShellAPI.DragAcceptFiles(TreeView1.Handle, True);
And the two procedure are these:
// Al arrastrar ficheros sobre el TV. On drop files to TV
procedure TForm1.FilesDrop(var Msg: TWMDROPFILES);
var
i:Integer;
DroppedFilename:string;
numFiles : longInt;
buffer : array[0..MAX_PATH] of char;
begin
// NĂºmero de ficheros arrastrados // Number of files
numFiles := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0) ;
// Recorrido por todos los arrastrados // Accept all files
for i := 0 to (numFiles - 1) do begin
DragQueryFile(Msg.Drop, i, #buffer, sizeof(buffer));
// Proteccion
try
DroppedFilename := buffer;
// HERE you can do something with the file...
TreeView1.Items.AddChild(nil, DroppedFilename);
except
on E:Exception do begin
// catch
end;
end;
end;
end;
procedure TForm1.EditWindowProc(var Msg: TMessage);
begin
// if correct message, execute the procedure
if Msg.Msg = WM_DROPFILES then begin
FilesDrop(TWMDROPFILES(Msg))
end
else begin
// in other case do default...
originalEditWindowProc(Msg) ;
end;
end;
I hope that this are usefull for you.
Regards.

Resources