Freeing arrays at procedure exit?

Just starting out? Need help? Post your questions and find answers here.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Freeing arrays at procedure exit?

Post by MachineCode »

Do I need to explicitly call FreeArray() when arrays are used in a procedure, and the procedure exits? The manual doesn't say. The arrays are not global or shared; they're just created with "Dim" at the start of the procedure, and I don't need to preserve their contents when the procedure exits.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Freeing arrays at procedure exit?

Post by Trond »

It's not needed.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Freeing arrays at procedure exit?

Post by MachineCode »

Thank you, Trond. Perhaps the manual could be updated in the FreeArray() entry to reflect this fact.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
shadow
User
User
Posts: 34
Joined: Thu Dec 16, 2010 1:25 pm
Location: Germany

Re: Freeing arrays at procedure exit?

Post by shadow »

FreeArray() is used du reuse the same array variable name for a new dimensioned array, like ReAllocMemory().
BTW all allocated memory is freed up when the program is closed with End.

If I'm on the wrong way please correct me :wink:
ThinkPad T61 | PureBasic 4.51 | Windows 7 (32) | Kubuntu 11.10 (64) | Syllable (32)
User avatar
Zefz
New User
New User
Posts: 8
Joined: Wed Mar 30, 2011 11:41 pm
Location: Norway

Re: Freeing arrays at procedure exit?

Post by Zefz »

A related question. Take a look at this code:

Code: Select all

Structure MyStruct
   *MemoryAddress
   VarA.w
   VarB.w
EndStructure

Global Dim MyArray.MyStruct(10)

For i = 0 To 10
  MyArray(i)\MemoryAddress = AllocateMemory(2048)
Next

FreeArray( MyArray() )
Is there a memory leak now because I did not FreeMemory() on each *MemoryAddress in the array? Or does FreeArray automatically handle this for us?
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Freeing arrays at procedure exit?

Post by Demivec »

Zefz wrote:A related question. Take a look at this code:

Code: Select all

Structure MyStruct
   *MemoryAddress
   VarA.w
   VarB.w
EndStructure

Global Dim MyArray.MyStruct(10)

For i = 0 To 10
  MyArray(i)\MemoryAddress = AllocateMemory(2048)
Next

FreeArray( MyArray() )
Is there a memory leak now because I did not FreeMemory() on each *MemoryAddress in the array? Or does FreeArray automatically handle this for us?
Yes there would be a memory leak. A pointer is not an object. If your example was written a little differently all the pointers in the array could be pointing to the same object or to objects that will persist even after the array's memory is freed.

FreeArray() does not handle memory that is allocated in addition to the space needed for the storage of an array's elements (strings being a notable exception) and the array's bookkeeping (i.e. dimensions, and element size). This means that it will free the memory needed to store the pointer, not the memory it may point to.

I thought there was a mention of this in the manual but I wasn't able to locate a quote to confirm this.
Post Reply