Memory Leaking in this code....

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Okay Timo, I took an example and modified it at bit from a previous thread where we discussed this, the memory still leaks, check this out:

Global ButtonGadget

OpenWindow(1, 128, 96, 320, 256, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "Background image example")
CreateGadgetList(WindowID())
CreateImage(1, WindowWidth(), WindowHeight())
StartDrawing(ImageOutput())
Box(0, 0, WindowWidth(), WindowHeight(), $ff)
FrontColor(0, 0, 0)
DrawingMode(1)
DrawingFont(LoadFont(0, "Times New Roman", 16))
Locate(64, 32)
DrawText("Background image")
StopDrawing()
ImageGadget(1, 0, 0, WindowWidth(), WindowHeight(), UseImage(1))
ButtonGadget = ButtonGadget(0, 80, 64, 160, 128, "My Button")

Repeat
EventID = WaitWindowEvent()
If EventID = #PB_EventGadget
Select EventGadgetID()

Case 0
Gosub UpdateWindow

EndSelect
EndIf


Until WaitWindowEvent()=#PB_EventCloseWindow
End

UpdateWindow:
UseImage(1)
StartDrawing(ImageOutput())
Box(0, 0, WindowWidth(), WindowHeight(), $ff)
FrontColor(0, 0, 0)
DrawingMode(1)
DrawingFont(LoadFont(0, "Times New Roman", 16))
Locate(64, 32)
DrawText("Test image")
StopDrawing()
SetGadgetState(1, UseImage(1))
ButtonGadget = ButtonGadget(0, 80, 64, 160, 128, "My Button")
Return
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

I would not use LoadFont() inside the UpdateWindow Code, as it accesses the Disk every time and makes your prog slow. This also leaks the memory a bit.
use LoadFont() only once, and then take UseFont() to get the right fontid.

Another thing is the Buttongadget:
When two gadgets are created with the same Number, the first one isn't freed. So you always create a new Buttongadget and everyone takes some memory.

to put the window up front again, use ActivateGadget()

This Code doesn't leak:

Code: Select all

OpenWindow(1, 128, 96, 320, 256, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "Background image example")
CreateGadgetList(WindowID())
CreateImage(1, WindowWidth(), WindowHeight())
StartDrawing(ImageOutput())
Box(0, 0, WindowWidth(), WindowHeight(), $ff)
FrontColor(0, 0, 0)
DrawingMode(1)
DrawingFont(LoadFont(0, "Times New Roman", 16))
Locate(64, 32)
DrawText("Background image")
StopDrawing()
ImageGadget(1, 0, 0, WindowWidth(), WindowHeight(), UseImage(1))
ButtonGadget = ButtonGadget(0, 80, 64, 160, 128, "My Button")

LoadFont(0, "Times New Roman", 16)

Repeat
EventID = WaitWindowEvent()
If EventID = #PB_EventGadget
Select EventGadgetID()

Case 0
Gosub UpdateWindow

EndSelect
EndIf


Until WaitWindowEvent()=#PB_EventCloseWindow
End

UpdateWindow:
UseImage(1)
StartDrawing(ImageOutput())
Box(0, 0, WindowWidth(), WindowHeight(), $ff)
FrontColor(0, 0, 0)
DrawingMode(1)
DrawingFont(UseFont(0))
Locate(64, 32)
DrawText("Test image")
StopDrawing()
SetGadgetState(1, UseImage(1))
ActivateGadget(0)
Return

---
Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Ahhh... I understand, you are a master teacher Timo, thank you.

Below is the code modified so to create a button on the Update Screen routine so it doesn't blink.

OpenWindow(1, 128, 96, 320, 256, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "Background image example")
CreateGadgetList(WindowID())
CreateImage(1, WindowWidth(), WindowHeight())
StartDrawing(ImageOutput())
Box(0, 0, WindowWidth(), WindowHeight(), $ff)
FrontColor(0, 0, 0)
DrawingMode(1)
DrawingFont(LoadFont(0, "Times New Roman", 16))
Locate(64, 32)
DrawText("Background image")
StopDrawing()
ImageGadget(1, 0, 0, WindowWidth(), WindowHeight(), UseImage(1))
ButtonGadget = ButtonGadget(0, 80, 64, 160, 128, "My Button")

LoadFont(0, "Times New Roman", 16)

Repeat
EventID = WaitWindowEvent()
If EventID = #PB_EventGadget
Select EventGadgetID()

Case 0
Gosub UpdateWindow

EndSelect
EndIf


Until WaitWindowEvent()=#PB_EventCloseWindow
End

UpdateWindow:
FreeGadget(0)
UseImage(1)
StartDrawing(ImageOutput())
Box(0, 0, WindowWidth(), WindowHeight(), $ff)
FrontColor(0, 0, 0)
DrawingMode(1)
DrawingFont(UseFont(0))
Locate(64, 32)
DrawText("Test image")
StopDrawing()
ButtonGadget = ButtonGadget(0, 80, 64, 160, 128, "My Button")
SetGadgetState(1, UseImage(1))
ActivateGadget(0)
Return
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

You could also do it like this:

Code: Select all

OpenWindow(1, 128, 96, 320, 256, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget, "Background image example")
CreateGadgetList(WindowID())
CreateImage(1, WindowWidth(), WindowHeight())
StartDrawing(ImageOutput())
Box(0, 0, WindowWidth(), WindowHeight(), $ff)
FrontColor(0, 0, 0)
DrawingMode(1)
DrawingFont(LoadFont(0, "Times New Roman", 16))
Locate(64, 32)
DrawText("Background image")
StopDrawing()
ImageGadget(1, 0, 0, WindowWidth(), WindowHeight(), UseImage(1))
ButtonGadget = ButtonGadget(0, 80, 64, 160, 128, "My Button")

LoadFont(0, "Times New Roman", 16)

Repeat
  EventID = WaitWindowEvent()
  If EventID = #PB_EventGadget
    Select EventGadgetID()
      Case 0
        Gosub UpdateWindow
    EndSelect
  EndIf

Until WaitWindowEvent()=#PB_EventCloseWindow
End

UpdateWindow:
UseImage(1)
StartDrawing(ImageOutput())
Box(0, 0, WindowWidth(), WindowHeight(), $ff)
FrontColor(0, 0, 0)
DrawingMode(1)
DrawingFont(UseFont(0))
Locate(64, 32)
DrawText("Test image")
StopDrawing()
SetGadgetState(1, UseImage(1))
RedrawWindow_(GadgetID(0), 0, 0, #RDW_INVALIDATE)
Return
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by geoff.

A question for pupil and/or freak

For my UpdateWindow routine I use:

StartDrawing(WindowOutput())
DrawImage(UseImage(1),0,0)
StopDrawing()

Wheras you create an image gadget and use:

SetGadgetState(1, UseImage(1))

Does your method have advantages or are the two methods equivalent?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

The advantage of the ImageGadget() way is that you don't have to redraw the image everytime the Window was put under another window for example.

Windows will do that for you, if you use ImageGadget()
That is the main reason why i use it.

Timo
Post Reply