Page 1 of 1

Threads and AllocateMemory

Posted: Mon Mar 20, 2023 8:32 pm
by sc4pb
In the docs it mentions that any memory allocated with AllocateMemory will be freed when the program ends. Of course instead we'll use FreeMemory when done with buffer. But I'm having some problems with AllocateMemory, pointers, and threads, all tangled together. Question:

If a procedure is launched as a new thread (CreateThread), and in that procedure AllocateMemory is used to create a buffer, and then the procedure (and thread) ends, is the buffer still allocated? Or was the memory released by PB when the thread ended?

Re: Threads and AllocateMemory

Posted: Mon Mar 20, 2023 8:37 pm
by NicTheQuick
The memory is still allocated because it is the same process. You can use the same memory block in different threads.

Re: Threads and AllocateMemory

Posted: Sun Mar 26, 2023 12:16 am
by RichAlgeni
Unless you use FreeMemory(), the memory is not released until the program ends. You can use that memory in different threads, as long as you keep the reference to it. If you lose the reference to the memory when you close a thread, the memory will still be allocated, but it will be unusable. The same goes with opening a file, or a database handle. These would all be called memory leaks.