If you forget to DEALLOCATE something allocated in FreeBasic

New to FreeBASIC? Post your questions here.
Vance
Posts: 20
Joined: Feb 26, 2006 3:23

If you forget to DEALLOCATE something allocated in FreeBasic

Post by Vance »

...is the block of memory reserved for the data lost forever, or is there a way to get that leaked memory back (rebooting the system, running some sort of test, etc.)?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Rebooting the system should do it :D I would check to make sure, but I don't want to lose an area of memory forever ^_~;;
tunginobi
Posts: 655
Joined: Jan 10, 2006 0:44
Contact:

Post by tunginobi »

I should hope that rebooting gives you back your memory. Imagine the chaos if we had to replace our 'leaked' memory with new RAM? :) That'd be a lot of money spent.

On a slightly more serious note, I am aware that Windows XP has mechanisms for freeing up memory leaks after a process terminates.
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Post by Fragmeister »

tunginobi wrote:I should hope that rebooting gives you back your memory. Imagine the chaos if we had to replace our 'leaked' memory with new RAM? :) That'd be a lot of money spent.

On a slightly more serious note, I am aware that Windows XP has mechanisms for freeing up memory leaks after a process terminates.
it does. When I'm writing/testing/debugging programs that use callocate, I usually don't deallocate the memory when i'm done until I have a workable program. Windows seems to do it for me. However, that's no reason not to use deallocate, and I believe that it should always be used. (just in case.)
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

The PE executable binary loader from Windows OS
The ELF executable binary loader from Linux OS

creates an process for the binary image after it exist it will be free all allocatet resourcec for you.

But it's nicer and saver for you to use deaalocate on your pointers in pair with oldpointer=NULL

if pointer=NULL then
print "sorry pointer are empty"
else
' do what ever with your pointer/s
end if

Joshy
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

tunginobi wrote:On a slightly more serious note, I am aware that Windows XP has mechanisms for freeing up memory leaks after a process terminates.
All versions of Windows NT had process garbage collection (they would release all resources that a process used after the termination of the process). This is why the requirements for NT OS's have always been a little higher.

9x may have a memory garbage collector, but that wouldn't have appeared until 98SE methinks.

In any event, NEVER rely on someone/something else to clean up after you. Always explicitly unload resources you have explicitly loaded.
tunginobi
Posts: 655
Joined: Jan 10, 2006 0:44
Contact:

Post by tunginobi »

1000101 wrote:
tunginobi wrote:On a slightly more serious note, I am aware that Windows XP has mechanisms for freeing up memory leaks after a process terminates.
In any event, NEVER rely on someone/something else to clean up after you. Always explicitly unload resources you have explicitly loaded.
I would have have added before that Windows XP's garbage collector was to be used as a safety net, but one would think that such a fact would be a given.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

tunginobi wrote:
1000101 wrote:In any event, NEVER rely on someone/something else to clean up after you. Always explicitly unload resources you have explicitly loaded.
...but one would think that such a fact would be a given.
You'd think so, but then you'd think people would RTM and STFW, but they don't so it's not a given :P
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Post by VirusScanner »

D.J.Peters, simply setting a pointer to NULL does nothing, in fact that is how you might lose memory. You must use the deallocate command to let the system know you are done with it.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

VirusScanner wrote:D.J.Peters, simply setting a pointer to NULL does nothing, in fact that is how you might lose memory. You must use the deallocate command to let the system know you are done with it.
So in other words, it might be a good idea to use deaalocate on your pointers in pair with oldpointer=NULL
;)
Vance
Posts: 20
Joined: Feb 26, 2006 3:23

Post by Vance »

From what I understand (keep in mind I'm still a beginner when it comes to using dynamic memory allocation in FreeBasic), is this the proper way to ensure there won't be a memory leak?

Code: Select all

'	Variable Init.
dim pointer1 as integer ptr

'	Allocate some memory for an integer.
pointer1=allocate(len(integer))

'	De-allocate memory assigned to integer so there's no leak.
deallocate pointer1

'	Set pointer1 to NULL so you don't accidentally point to an area of memory that was already de-allocated.
pointer1=NULL
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

Vance wrote:From what I understand (keep in mind I'm still a beginner when it comes to using dynamic memory allocation in FreeBasic), is this the proper way to ensure there won't be a memory leak?

Code: Select all

'	Variable Init.
dim pointer1 as integer ptr

'	Allocate some memory for an integer.
pointer1=allocate(len(integer))

'	De-allocate memory assigned to integer so there's no leak.
deallocate pointer1

'	Set pointer1 to NULL so you don't accidentally point to an area of memory that was already de-allocated.
pointer1=NULL
Basically, yeah. Don't forget, though, to do some stuff with your allocated memory between allocation and deallocation :P But that is obvious.

Further, in such an oversimplified example as you gave, it would be less memory just to allocate an integer in the normal contextual manner of the compiler (Dim As Integer foo) instead of a pointer to an integer.

Another important thing about pointers is knowing when to use them and when not to use them.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

yes, thats correct. keep in mind, the last step (ptr=NULL) is optional, you won't lose mem if you don't do it. it just helps tp prevent future errors if you null the ptr.
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Post by Fragmeister »

when you deallocate a ptr, isn't it automatically set to 0? (null)
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Fragmeister wrote:when you deallocate a ptr, isn't it automatically set to 0? (null)
...no ;p
Post Reply