End - All resources freed automatically?

Just starting out? Need help? Post your questions and find answers here.
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

End - All resources freed automatically?

Post by forumuser »

Hi,

sorry, I'm new to programming. Found PureBasic, looked at the help file, the forum, thought: Not too hard to read (the source code), helpful people around. Bought it...

My first naïve question:
Does: "End" terminate a program in such a way, that everything that was setup before like:

Code: Select all

BindEvent()
AddKeyboardShortcut()
AllocateMemory()
etc.
is freed automatically or is an exit procedure necessary, that needs to clean everything up before proceeding with "End"?
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: End - All resources freed automatically?

Post by walbus »

PB give all free automatically
Last edited by walbus on Thu Apr 19, 2018 1:29 pm, edited 3 times in total.
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Re: End - All resources freed automatically?

Post by forumuser »

So as long as I handle these two like so:

Code: Select all

    ; Memory
    *mem = AllocateMemory(<some size>)

    If *mem
      ; Do something
      FreeMemory(*mem)
    EndIf

    Global Dim Temp(10)
    ; Assign some value to it
    If ArraySize(Temp()) <> -1
      FreeArray(Temp())
    EndIf
I'm fine and don't have to worry about anything else?
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: End - All resources freed automatically?

Post by Marc56us »

:arrow: Help

Code: Select all

Syntax
    End [ExitCode]
Description
    Ends the program execution correctly.
Unless I'm mistaken, all resources (keyboard shortkeys, memory allocation etc) created by your program are released automatically (as in any other language)

It does not matter where the "End" instruction is (even in a procedure)

"Global" Keyword does not mean operating system variable.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: End - All resources freed automatically?

Post by Dude »

walbus wrote:Global Dim and AllocateMemory you must give free again
Global is used for your app variables; nothing more. When your app ends, the variables are gone (freed).

For AllocateMemory, the manual says: "[...] allocated memory blocks are automatically freed when the program ends."

So yes, "End" will clean both of these up.

Special note for threads: Threads can keep running even after "End" is executed and your exe no longer shows in the Task Manager. :shock: So, always end your threads correctly before "End". It's happened to me and I don't know what I did or how, but it can happen.
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: End - All resources freed automatically?

Post by Bisonte »

For AllocateMemory, the manual says: "[...] allocated memory blocks are automatically freed when the program ends."
With one exception: Memory that is allocated with or by WindowsAPI (I think this also applies to the other OS API).
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Fred
Administrator
Administrator
Posts: 16687
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: End - All resources freed automatically?

Post by Fred »

Windows automatically release memory as well. Threads are also closed on program end. To sum up: all is released automatically when program end, no need to do manual cleanup
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Re: End - All resources freed automatically?

Post by forumuser »

Thanks for all the answers, appreciate it!
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: End - All resources freed automatically?

Post by TI-994A »

It should also be noted that the End command is only required if the program needs to be explicitly terminated while an event loop is still running.

Simply breaking the active event loop would also essentially terminate the program and trigger all associated housekeeping tasks.

Code: Select all

OpenWindow(0, 100, 100, 250, 100, "Program Termination")
While WaitWindowEvent() <> #PB_Event_CloseWindow : Wend

;the program safely ends here when the While/Wend loop is broken
It's technically a super Break. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply