Threads and AllocateMemory

Just starting out? Need help? Post your questions and find answers here.
sc4pb
User
User
Posts: 26
Joined: Tue Mar 07, 2023 5:33 pm

Threads and AllocateMemory

Post 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?
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Threads and AllocateMemory

Post by NicTheQuick »

The memory is still allocated because it is the same process. You can use the same memory block in different threads.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Threads and AllocateMemory

Post 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.
Post Reply