Jump to content

Is Ar_Null enough to properly clear up an array that contains string values?


hexef

Recommended Posts

Consider this code:

 

array_var arTest

 

let arTest := Ar_List SunnyRef, 23.59, "RandomString"

 

let arTest := Ar_Null

 

Does the Ar_Null part properly destroys the "RandomString" string ?

 

Or do I need to do something like:

 

sv_Destruct arTest[2] ; which is the index to "RandomString"

 

let arTest := Ar_Null

Link to comment
Share on other sites

ar_null alone should be fine

destructing string variables is really about removing the link between the variable and the string in NVSE, not about cleaning up the actual string, which is something NVSE does on its own when a string is no longer referred to by anything (whether string variable or array element)

Link to comment
Share on other sites

That's really good to know. Now I gotta remove all the foreach instructions that only targeted the string vars for destruction from arrays and replace everything with just ar_null.

 

 

What about an array that is composed of arrays? Is ar_null safe when used only on the main array or do I have to foreach like how I did with string vars?

Edited by xqdcss
Link to comment
Share on other sites

If the sub-arrays are only referenced by the main array, then ar_nulling the main array suffices too. The same principle applies, in that an array var is only a reference to an array in NVSE. Both strings and array vars are really only ints, referring to the indices of the strings and arrays in NVSE. Destructing a string var or nulling an array var is only about severing that link. Once a string or array is no longer referenced by anything, NVSE cleans it up automatically and frees up its index in the next frame.

 

So if you park array A inside array B, you can safely null array var A because array A will still be referenced by B. Then, when A is only referred to by B, nulling array var B will cause NVSE to remove B if B is no longer referred to by anything else, and so the last remaining reference to A is removed, which causes A to get cleaned up as well.

By the same principle, you could park references to one and the same array in several array vars and elements of other arrays, and NVSE will still only contain that one array rather than several copies of it. It just keeps track of the references. You can see that if you ar_dump an array: the readout will specify how many references there are to it.

 

A string that was only ever part of an array, and not referenced by a string var, doesn't need destructing, because there was never a link between the string and a string var. Erasing the element from the array is enough to get rid of it, like any other value. The only reason we're so adamant about the need to destruct string vars ASAP is that they're not automatically destructed when used as local vars in a UDF when the UDF is done. It's quite easy to overlook, and I've had instances of forgetting about it myself, leading to bloat.

 

Also, if you erase elements from regular arrays while looping them, foreach loops aren't the best way of doing that, because the array rearranges itself when an element is erased so you'll skip elements or the loop itself can break. Use reverse while loops instead:

let iKey := ar_size YourArray
while -1 < (iKey -= 1)
   ; do or check whatever
   ar_erase YourArray, iKey
loop
Link to comment
Share on other sites

Thank you for having the patience to share this stuff. It cleared some things up for me, no doubt about that. So basically from what I understood, say I have something like this:

Ar_Append arMain Ar_List <something>
Ar_Append arMain Ar_List <something_else>
Ar_Append arMain Ar_List <some_other_things>

let arMain := Ar_Null

That ar_null deletes all the contents of arMain, because the sub-arrays are not referenced by anything else.

 

But if I have something like this:

Ar_Append arMain arSub1
Ar_Append arMain arSub2
Ar_Append arMain arSub3

let arMain := Ar_Null

This Ar_Null instance only "disassembles" arMain but the sub-arrays are not deleted because they are referenced by other array_var type of variables. Am I right?

 

Also,

 

if you erase elements from regular arrays while looping them, foreach loops aren't the best way of doing that, because the array rearranges itself when an element is erased so you'll skip elements or the loop itself can break.

 

 

That's good to know. I always forget that NVSE's arrays are automatically rearranged.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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