Clipboard Bug?

Just starting out? Need help? Post your questions and find answers here.
garretthylltun
Enthusiast
Enthusiast
Posts: 346
Joined: Wed Oct 26, 2005 2:46 am
Contact:

Clipboard Bug?

Post by garretthylltun »

System:
PureBasic 5.30 Beta 9 (Linux - x86)
Distro: elementary OS: 0.2.1 "Luna"(32-bit)
Based on: Ubuntu 12.04("Precise")
Intel Core 2 CPU 6300 @ 1.86ghz x 2
Memory: 2gb
Library/Command/Function involved:
Library: Clipboard
Command: GetClipboardText()
Symptom:
Continuously polling the clipboard with GetClipboardText() in a loop, with or without any time delay eventually causes program to lag and eventually stall for undesirable amounts of time. Amount of time until effect took place varied from a few minutes to 10 minutes.
Example Code:

Code: Select all

  OpenWindow(0,60,60,220,200,"Clipboard Bug?",#PB_Window_SystemMenu|#PB_Window_TitleBar)
  TextGadget(0,10,10,290,30,"0")
  ListViewGadget(1,10,50,200,140)
  vClipBoardData$ = GetClipboardText()
  ; ----------------------------------
  Repeat 
    vWindowEvent = WindowEvent()
    Delay(100)
    vClipBoardDataTemp$ = GetClipboardText()
    If vClipBoardDataTemp$ <> vClipBoardData$
      vClipBoardData$ = vClipBoardDataTemp$
      AddGadgetItem(1,0,vClipBoardData$)
    EndIf
    SetGadgetText(0,Str(ElapsedMilliseconds()))
  Until vWindowEvent = #PB_Event_CloseWindow
Notes:
I do not have any other systems to test this on or against, so I can't confirm if it's a system specific issue. But, doing the same exact thing in another language I used to use under Wine doesn't suffer from this.. I don't have any other Linux based programming languages to test with right now either or I would have.

I actually have written this code several times using different coding styles, Goto, Gosub, Function, Procedure and just right in the repeat loop as well, all with the same results. I also let the code run while I was interacting with other programs as well as let it run without my interaction with anything else at all and the results were the same. What I didn't try was clearing the clipboard to see if the effect would still happen with an empty clipboard.

What I was trying to accomplish is to keep an eye on the clipboard for text, which was to be the title of software, test that text with a regex against a small database file of software titles to see if the title existed already in the database.
Best regards,
~Garrett
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
· Necroprogramming FTW! - "Wait.. Is necroprogramming legal?"
· http://www.freewarehome.com/ <-- Freeware listings since 1996
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Clipboard Bug?

Post by Danilo »

Possibly your event queue gets full? You only process 1 event on every loop, and with the Delay(100) you process
only 10 events per second. Maybe the AddGadgetItem() produces several events (like updating/redrawing etc).

Try catching all events for every loop, or even better: Move the clipboard polling to a timer with 100ms.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Clipboard Bug?

Post by infratec »

Or use

Code: Select all

vWindowEvent = WaitWindowEvent(100)
and remove the Delay(100)

Bernd
garretthylltun
Enthusiast
Enthusiast
Posts: 346
Joined: Wed Oct 26, 2005 2:46 am
Contact:

Re: Clipboard Bug?

Post by garretthylltun »

Will try the suggestions.

BTW..I may have been slightly premature in my post. I could swear that I had no userlibs sitting in there, but did another check... After The Post and found 3 userlibs still left in there. removed them and am currently running the program now.

<---- feels like an old stupid coot now.
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
· Necroprogramming FTW! - "Wait.. Is necroprogramming legal?"
· http://www.freewarehome.com/ <-- Freeware listings since 1996
garretthylltun
Enthusiast
Enthusiast
Posts: 346
Joined: Wed Oct 26, 2005 2:46 am
Contact:

Re: Clipboard Bug?

Post by garretthylltun »

After clearing the userlibs and restarting the compiler, I ran the program for near 40 minutes without any interaction by myself on the computer. Came back and started to randomly copy single words of text from various programs. After about 5 minutes, the lag effect began and was shortly followed by being stalled for 30 to 60 seconds.

I have now changed the code to remove the delay and use the WaitWindowEvent(100)

Code: Select all

  OpenWindow(0,60,60,220,200,"Clipboard Bug?",#PB_Window_SystemMenu|#PB_Window_TitleBar)
  TextGadget(0,10,10,290,30,"0")
  ListViewGadget(1,10,50,200,140)
  vClipBoardData$ = GetClipboardText()
  ; ----------------------------------
  Repeat 
    vWindowEvent = WaitWindowEvent(100)
    vClipBoardDataTemp$ = GetClipboardText()
    If vClipBoardDataTemp$ <> vClipBoardData$
      vClipBoardData$ = vClipBoardDataTemp$
      AddGadgetItem(1,0,vClipBoardData$)
      SetGadgetState(1,0)
    EndIf
    SetGadgetText(0,Str(ElapsedMilliseconds()))
  Until vWindowEvent = #PB_Event_CloseWindow
BTW, I've been running the program in compiled form and not from the IDE.

Thanks,
~Garrett
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
· Necroprogramming FTW! - "Wait.. Is necroprogramming legal?"
· http://www.freewarehome.com/ <-- Freeware listings since 1996
garretthylltun
Enthusiast
Enthusiast
Posts: 346
Joined: Wed Oct 26, 2005 2:46 am
Contact:

Re: Clipboard Bug?

Post by garretthylltun »

BTW, just an off the side observation.

Delay(100) = 1% cpu
WaitWindowEvent(100) = 45% cpu

About 5 or 10 minutes into latest run and have already experience a short stall and am noticing some slight delays in the looping now.
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
· Necroprogramming FTW! - "Wait.. Is necroprogramming legal?"
· http://www.freewarehome.com/ <-- Freeware listings since 1996
garretthylltun
Enthusiast
Enthusiast
Posts: 346
Joined: Wed Oct 26, 2005 2:46 am
Contact:

Re: Clipboard Bug?

Post by garretthylltun »

Alright, done testing. Even after changes, emptied userlibs, compiler restart, program recompiled, it still suffers from occasional lag and stalls. I also tested with a time log of any delays of over 200ms. the lags usually lasted 200 to 300ms and the stalls typically lasted a minimum of 31000ms. Also tested while doing file intense operations on the system, such as decompressing a 4 gig tar file and trying to load that 4 gig xml file into an editor. During the decompressing the test program suffered no lags or stalls, but during the attempted loading of the 4 gig xml file into an editor the test program was riddled with lag hits and a few stalls.

Unless anyone else is able to use the following code to reproduce this problem, then I will have to mark it up as my system.

Code: Select all

  OpenWindow(0,60,60,220,200,"Clipboard Bug?",#PB_Window_SystemMenu|#PB_Window_TitleBar)
  TextGadget(0,10,10,290,30,"0")
  ListViewGadget(1,10,50,200,140)
  AddGadgetItem(1,0,"[ Start time:  " + FormatDate("%hh:%ii:%ss", Date()) + " ]")
  vClipBoardData$ = GetClipboardText()
  ; ----------------------------------
  Repeat 
    vWindowEvent = WaitWindowEvent(100)
    vTimeCurr = ElapsedMilliseconds()
    vTimeDiff = vTimeCurr - vTimePrev
    vClipBoardDataTemp$ = GetClipboardText()
    If vTimeDiff > 200
      AddGadgetItem(1,0,"[ " + FormatDate("%hh:%ii:%ss", Date()) + " ]-[ " + Str(vTimeDiff) + " ]")
      SetGadgetState(1,0)
    EndIf
    If vClipBoardDataTemp$ <> vClipBoardData$
      vClipBoardData$ = vClipBoardDataTemp$
      AddGadgetItem(1,0,vClipBoardData$)
      SetGadgetState(1,0)
    EndIf
    SetGadgetText(0,Str(ElapsedMilliseconds()))
    vTimePrev = vTimeCurr
  Until vWindowEvent = #PB_Event_CloseWindow


Best regards,
~Garrett
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
· Necroprogramming FTW! - "Wait.. Is necroprogramming legal?"
· http://www.freewarehome.com/ <-- Freeware listings since 1996
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Clipboard Bug?

Post by Danilo »

Could you please test using timers? In the following code, GetClipboardText() is only called every 100 milliseconds.

Code: Select all

OpenWindow(0,60,60,220,200,"Clipboard Bug?",#PB_Window_SystemMenu|#PB_Window_TitleBar)
TextGadget(0,10,10,290,30,"0")
ListViewGadget(1,10,50,200,140)
AddGadgetItem(1,0,"[ Start time:  " + FormatDate("%hh:%ii:%ss", Date()) + " ]")

vClipBoardData$ = GetClipboardText()

AddWindowTimer(0,0,100)
AddWindowTimer(0,1,20)

; ----------------------------------
Repeat 
    vWindowEvent = WaitWindowEvent()
    If vWindowEvent = #PB_Event_Timer
        Select EventTimer()
            Case 0
                vClipBoardDataTemp$ = GetClipboardText()
                If vClipBoardDataTemp$ <> vClipBoardData$
                    vClipBoardData$ = vClipBoardDataTemp$
                    time.s = "[ " + FormatDate("%hh:%ii:%ss", Date()) + "] "
                    AddGadgetItem(1,0,time + vClipBoardData$)
                    SetGadgetState(1,0)
                EndIf
            Case 1
                SetGadgetText(0,Str(ElapsedMilliseconds()))
        EndSelect
    EndIf
Until vWindowEvent = #PB_Event_CloseWindow
BTW: WaitWindowEvent(100) does not mean it is always waiting 100 milliseconds. If events arrive, they get
processed immediately. It just waits 100ms when no events occur. That means, with your old code, GetClipboardText()
could get called every millisecond (or more), if any events (f.e. mouse movement) arrive. With timers you don't need that.

When loading a 4GB file all-at-once into memory, a lag could occur when you run out of memory. In this case, the system could start to
swap out memory to HDD (swap partition on Linux). It depends on how much memory your PC has, and if you are using 32bit or 64bit Linux OS,
and 32bit or 64bit PureBasic (in your case, your process does not require much memory, it is just the strings that get added to the ListViewGadget
that add memory over time). That would be a system issue, and other programs should also show the lag, if it gives priority to the loading
of the big 4GB file and starts to swap out memory of other processes.
garretthylltun
Enthusiast
Enthusiast
Posts: 346
Joined: Wed Oct 26, 2005 2:46 am
Contact:

Re: Clipboard Bug?

Post by garretthylltun »

Will test with your code in the morning.
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
· Necroprogramming FTW! - "Wait.. Is necroprogramming legal?"
· http://www.freewarehome.com/ <-- Freeware listings since 1996
garretthylltun
Enthusiast
Enthusiast
Posts: 346
Joined: Wed Oct 26, 2005 2:46 am
Contact:

Re: Clipboard Bug?

Post by garretthylltun »

@Danilo

Testing with the timers almost alleviated the problem. The program did not suffer the little lags, but after two hours of running and some casual use on my computer, it eventually was hit with two stalls a few minutes apart, otherwise, this was far better than any of my previous results.

I'm still not sure if this is a bug or something to do with my system. It's a pretty clean system, in fact, I only recently wiped this computer and installed Linux on it. While it's based on a slightly older version of Ubuntu, it is updated with all the latest updates available for this distro. So, unless anyone else is able to get similar results to mine, I just have no explanation, ideas or conclusions as to what the issue is.

Thanks to all of you who have tried to help me pinpoint this,
~Garrett
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
· Necroprogramming FTW! - "Wait.. Is necroprogramming legal?"
· http://www.freewarehome.com/ <-- Freeware listings since 1996
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Clipboard Bug?

Post by freak »

Its not a bug. Your problem doesn't even have anything to do with the clipboard functions.

If you use the example from the first post and comment out all clipboard stuff, the problem will be even worse. You are simply generating more events that you are handling because of the SetGadgetText() call.

On each iteration, your mainloop does this:
1) consume one event with WindowEvent()
2) call SetGadgetText() which will produce more events (for redrawing etc).

This way, the events keep piling up until the program seems to hang. Your later example with WaitWindowEvent() and a timeout still has the same problem: for each processed event, more events are generated.

This is why the timer examply by Danilo works: Now, SetGadgetText() is called less times than events are being processed, so the event queue has a chance to catch up.
quidquid Latine dictum sit altum videtur
User avatar
Derren
Enthusiast
Enthusiast
Posts: 316
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Clipboard Bug?

Post by Derren »

Ever since playing Minecraft, I have cursed Java's garbage collector, because programmers don't take care of their useless data anymore and when the GC kicks in, it slows down the program immensely. But now see what happens when programmers don't take care of their data and there is NO garbage collector... :?
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Clipboard Bug?

Post by freak »

A garbage collector wouldn't help you here because the events are not garbage. They are waiting in the queue to be processed.
quidquid Latine dictum sit altum videtur
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Clipboard Bug?

Post by skywalk »

Thanks for clarifying.
When PB returns/exits a Procedure that created arrays or big strings internally, are those freed immediately or at program termination?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Fred
Administrator
Administrator
Posts: 18154
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Clipboard Bug?

Post by Fred »

They are freed immediately on procedure exits.
Post Reply