Worst Heisenbug ever? [solved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Erich
User
User
Posts: 49
Joined: Thu Sep 30, 2010 9:21 pm

Worst Heisenbug ever? [solved]

Post by Erich »

Okay, folks, I might have stumbled over the worst "Heisenbug" ever. I'm drawing text with graphics acceleration to ScreenOutput() in a screen obtained OpenWindowedScreen().

- it works fine if it's not in a DLL
- if it's in a DLL and loaded from that DLL, then any call to DrawText() in my Linux 64 bit machine increases memory; no other drawing functions cause a memory leak
- But when I try a minimal example, the example works in a DLL!

Minimal example:

Code: Select all

Procedure mainLoop()
  If OpenWindow(0,20,20,800,600,"DrawText Memory Leak",#PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
    InitSprite()
    LoadFont(0, "Glass TTY VT220", 14)
    If OpenWindowedScreen(WindowID(0),0,0,800,600)
      done=#False
      Repeat
        If StartDrawing(ScreenOutput())
          DrawingFont(FontID(0))
          s$=""
          For i=1 To 60
            s$=s$+Chr(Random(96)+32)
          Next
          DrawText(40,40, s$,RGB(Random(255),Random(255),Random(255)),RGB(0,0,0))
          StopDrawing()
          FlipBuffers()
        EndIf
        Repeat
          Event = WindowEvent()
          If Event=#PB_Event_CloseWindow
            done=#True
          EndIf
        Until event=0
        Delay(1)
      Until done
    EndIf
    CloseWindow(0)
  EndIf
EndProcedure

ProcedureDLL xMain()
  mainLoop()
EndProcedure
with the following to load:

Code: Select all

Import "main.so"
xMain()
EndImport

CallCFunctionFast(@xMain())
This works! :lol: (By the way, the font doesn't matter, you can remove it. It's only there because I first thought the font is the culprit. It's not.)

Now the following non-minimal, incomplete real-life code does not work:

Code: Select all

Procedure DrawVram ()
  Protected i,wFactor,hFactor,x,y
  Protected.f cwOffset
  Protected.q ms
  Protected s$
  If StartDrawing(ScreenOutput())
    If GetSpecial(#CLEAN_BACKGROUND)=#Background_Clean
      Box(0,0,w*charwidth,h*charheight,getSpecial(#BACKGROUND))
    EndIf
   DrawingFont(FontID(#MainFont))
    For i=0 To maxPos
      wFactor=i%w
      hFactor=i/w
      Box(charwidth*wFactor,charheight*hFactor,charwidth,charheight,GetDirectBackColor(i))
      s$=Chr(GetDirectChar(i))
      cwOffset=Max((charwidth-TextWidth(s$))/2,0)
      DrawText(charwidth*wFactor+cwOffset,charheight*hFactor,s$,GetDirectColor(i),GetDirectBackColor(i))
    Next
    If CursorShown()
      If GetSpecial(#CURSOR_TYPE)=#Cursor_Overwrite
        Box(GetCursorColumn()*charwidth, GetCursorLine()*charheight, charwidth, charheight,GetColor(GetCursorColumn(), GetCursorLine()))
        s$=Chr(GetChar(GetCursorColumn(), GetCursorLine()))
        cwOffset=Max((charwidth-TextWidth(s$))/2,0)
      ;  DrawText(charwidth*GetCursorColumn()+cwOffset,charheight*GetCursorLine(),s$, GetBackColor(GetCursorColumn(), GetCursorLine()), GetColor(GetCursorColumn(), GetCursorLine()))
      Else
        Box(charwidth*GetCursorColumn()-1,charheight*GetCursorLine(), 2, charheight, GetColor(GetCursorColumn(), GetCursorLine()))
      EndIf
    EndIf
    StopDrawing()
    FlipBuffers()
    needsRefresh=#False
  EndIf
EndProcedure
Within a DLL it leaks memory, several MBs per second! Okay, looks complicated, so maybe there is an error in it, right? But when I comment out DrawText(), it does not leak memory. If I replace the more complicated call to DrawText() with

Code: Select all

DrawText(0,0,"Hello world")
then it does leak memory, so it's the call to DrawText() that causes the leak. Something in my real-life code seems to cause a memory leak in combination with DrawText(), but the minimal example shows that it cannot be just DrawText() alone. But I'm not doing anything special, I'm just reading characters and colors from three global arrays! This is driving me nuts. :evil:

Does anybody have an idea what could go wrong here?

__________________________________________________
Spaces added
06.03.2018
RSBasic
Last edited by Erich on Thu Feb 22, 2018 5:15 pm, edited 1 time in total.
"I have never let my schooling interfere with my education." - Mark Twain
User avatar
Erich
User
User
Posts: 49
Joined: Thu Sep 30, 2010 9:21 pm

Re: Worst Heisenbug ever?

Post by Erich »

Nevermind, I've updated from PB 5.61 to PB 5.62 and miraculously the memory leak seems to be gone. My day is saved! :mrgreen:
"I have never let my schooling interfere with my education." - Mark Twain
Post Reply