View Issue Details

IDProjectCategoryView StatusLast Update
0037809LazarusLCLpublic2020-09-25 17:29
ReporterCyrax Assigned To 
PrioritynormalSeverityminorReproducibilityrandom
Status newResolutionopen 
PlatformLinux x86_64OSArch 
Product Version2.1 (SVN) 
Summary0037809: When TForm.Position value is poScreenCenter, a form (either modal or normal one) is sometimes not centred properly.
DescriptionLike summary says, this bug causes forms not to be where the TForm.Position property dictates.
Steps To Reproduce0. Create new application project.
1. Set form property Position to poScreenCenter.
2. Run the project multiple times until this bug manifests itself.
Tagspatch
Fixed in Revision
LazTarget
WidgetsetGTK 2
Attached Files

Activities

Cyrax

2020-09-24 17:48

reporter  

Cyrax

2020-09-25 10:24

reporter   ~0125833

Attached a test project.
project1.zip (128,204 bytes)

Cyrax

2020-09-25 10:28

reporter   ~0125834

Attached patch that fixes this bug.
gtk2widgetset.inc.diff (788 bytes)   
diff --git a/lcl/interfaces/gtk2/gtk2widgetset.inc b/lcl/interfaces/gtk2/gtk2widgetset.inc
index e3ae472e54..29b6e576a9 100644
--- a/lcl/interfaces/gtk2/gtk2widgetset.inc
+++ b/lcl/interfaces/gtk2/gtk2widgetset.inc
@@ -6540,6 +6540,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        Sleep(1);
       end;
 
       if ALoop > AMaxLoops then
@@ -6559,6 +6560,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        Sleep(1);
       end;
 
       if ALoop > AMaxLoops then
@@ -6580,6 +6582,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        Sleep(1);
       end;
 
gtk2widgetset.inc.diff (788 bytes)   

Zeljan Rikalo

2020-09-25 10:50

developer   ~0125835

I've already fixed same bug under Qt/Qt5 widgetsets some time ago (r62970 and some later probably, think that I've also fixed gtk2 in same way). Basically I don't like this patch with sleep() involved. Problem is in accurate widget frame under x11.

Cyrax

2020-09-25 11:44

reporter   ~0125838

With the patch, I always get the form positioned at the middle of screen in each run of the test project. I suspect that there is thread race condition or something similar manifesting inside GTK libraries as adding sleep() solves this bug. Probably GTK library (and widget) needs just time to initialize its internal structures in timely order.

Sleep can be changed to equivalent GTK one.

Cyrax

2020-09-25 13:33

reporter   ~0125845

Hmm, it still goes off from the centre with the patch but not so drastically.

Zeljan Rikalo

2020-09-25 13:47

developer   ~0125847

So, patch is not good - it does not fix the problem.

Cyrax

2020-09-25 13:58

reporter   ~0125848

Attached patch should fix this bug for good. After 60 runs, the test program stays with centre of the screen.
gtk2widgetset.inc-3.diff (1,043 bytes)   
diff --git a/lcl/interfaces/gtk2/gtk2widgetset.inc b/lcl/interfaces/gtk2/gtk2widgetset.inc
index e3ae472e54..c86c08d080 100644
--- a/lcl/interfaces/gtk2/gtk2widgetset.inc
+++ b/lcl/interfaces/gtk2/gtk2widgetset.inc
@@ -6478,6 +6478,8 @@ end;
 
 function TDummyWidget.ShowDummyWidget(const ALeft, ATop, AWidth,
   AHeight: integer): boolean;
+const
+  G_USLEEP_DELAY = 400;
 var
   R: TRect;
   {$IFDEF DEBUGGTK2FRAMESIZE}
@@ -6540,6 +6542,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        g_usleep(G_USLEEP_DELAY);
       end;
 
       if ALoop > AMaxLoops then
@@ -6559,6 +6562,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        g_usleep(G_USLEEP_DELAY);
       end;
 
       if ALoop > AMaxLoops then
@@ -6580,6 +6584,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        g_usleep(G_USLEEP_DELAY);
       end;
 
gtk2widgetset.inc-3.diff (1,043 bytes)   

Cyrax

2020-09-25 15:02

reporter   ~0125849

Hmm, it seems that I need increase value of G_USLEEP_DELAY constant a little. A new patch with the change is attached in this post.
gtk2widgetset.inc-4.diff (1,043 bytes)   
diff --git a/lcl/interfaces/gtk2/gtk2widgetset.inc b/lcl/interfaces/gtk2/gtk2widgetset.inc
index e3ae472e54..c86c08d080 100644
--- a/lcl/interfaces/gtk2/gtk2widgetset.inc
+++ b/lcl/interfaces/gtk2/gtk2widgetset.inc
@@ -6478,6 +6478,8 @@ end;
 
 function TDummyWidget.ShowDummyWidget(const ALeft, ATop, AWidth,
   AHeight: integer): boolean;
+const
+  G_USLEEP_DELAY = 600;
 var
   R: TRect;
   {$IFDEF DEBUGGTK2FRAMESIZE}
@@ -6540,6 +6542,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        g_usleep(G_USLEEP_DELAY);
       end;
 
       if ALoop > AMaxLoops then
@@ -6559,6 +6562,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        g_usleep(G_USLEEP_DELAY);
       end;
 
       if ALoop > AMaxLoops then
@@ -6580,6 +6584,7 @@ begin
       begin
         if not g_main_context_iteration(g_main_context_default, False) then
           break;
+        g_usleep(G_USLEEP_DELAY);
       end;
 
gtk2widgetset.inc-4.diff (1,043 bytes)   

Zeljan Rikalo

2020-09-25 17:29

developer   ~0125863

From gtk docs: g_usleep() may have limited precision, depending on hardware and operating system; don't rely on the exact length of the sleep. So such bug can raise again at any time

Issue History

Date Modified Username Field Change
2020-09-24 17:48 Cyrax New Issue
2020-09-24 17:48 Cyrax File Added: wrong position of form at runtime.png
2020-09-25 10:24 Cyrax Note Added: 0125833
2020-09-25 10:24 Cyrax File Added: project1.zip
2020-09-25 10:28 Cyrax Note Added: 0125834
2020-09-25 10:28 Cyrax File Added: gtk2widgetset.inc.diff
2020-09-25 10:28 Cyrax Tag Attached: patch
2020-09-25 10:50 Zeljan Rikalo Note Added: 0125835
2020-09-25 11:44 Cyrax Note Added: 0125838
2020-09-25 13:33 Cyrax Note Added: 0125845
2020-09-25 13:47 Zeljan Rikalo Note Added: 0125847
2020-09-25 13:58 Cyrax Note Added: 0125848
2020-09-25 13:58 Cyrax File Added: gtk2widgetset.inc-3.diff
2020-09-25 15:02 Cyrax Note Added: 0125849
2020-09-25 15:02 Cyrax File Added: gtk2widgetset.inc-4.diff
2020-09-25 17:29 Zeljan Rikalo Note Added: 0125863