Jump to content

[LE] Are papyrus arrays garbage collected?


Recommended Posts

Hello,

 

Is it necessary from the coder's end to free arrays allocated with "new" in papyrus? Or are they garbage collected?

What I mean, is basically: is the following code leaking memory, or is this fine?

Int[] myarray = new Int[10]
myarray = new Int[20]
myarray = new Int[20]
myarray = new Int[20]
 
; Were the first 3 allocations freed automatically, or am I leaking memory?

Thanks

Edited by WhiteWolf424242
Link to comment
Share on other sites

If you read the whole article here https://www.creationkit.com/index.php?title=Arrays_(Papyrus) you'll see this headline:

 

"Assigning/Passing Arrays"

 int[] Array1 = new int[5]
 int[] Array2 = new int[10]
 Array2 = Array1             ; Array1 and Array2 now look at the same 5 element array, and Array2s original 10 element array is destroyed. 

The same applies to your sample which is using the same array variable.

 

In case you want to destroy an array, which is a script variable or pre-filled as property look at these (it works for each array type like actor[], form[], string[], float[], ..):

Int[] PROPERTY myArray auto        ; pre-filled by CK
Int[] a


EVENT OnInit()
    a = new Int[128]               ; script variable has been initialized
    Debug.Trace(" Array length is " + a.Length)        ; 128

    DestroyArray()
    Debug.Trace(" Array length is " + a.Length)       ; Zero
ENDEVENT


FUNCTION DestroyArray()
;----------------------
    int[] b                       ; set undefined array function variable of same type

    a = b                         ; destroy script variable
    myArray = b                   ; destroy property as well
ENDFUNCTION
Edited by ReDragon2013
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...