Destroying a struct

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,342
Hi,

I know when you call struct.destroy() it sets up a free spot in the actual array of integers and so when a new struct is made, that spot can be used up.

But I am still confused on this behavior below. Suppose I have some data structure which has struct members (an array or another struct). I then make a local struct, and set it to that member. What happens when I call destroy on the local struct afterwards? Will it also be the same as calling destroy on that actual struct member?

I ask this because I've had some funny behavior with some code. When I use local structs and destroy them, my global structs get messed up (the data gets overwritten). When I stopped destroying the local structs, my global structs worked fine. So I am guessing that you should not call destroy on a local struct unless you want its "reference" destroyed also.

JASS:
function foo takes myStruct s returns nothing
   local myStruct bar
   set bar = s
   call bar.destroy()
endfunction

So, would these be equivalent then, since they are the same underlying pointer/integer in the array managing myStruct?

JASS:
//s is some instance of myStruct
call foo(s)
call s.destroy()
 
Yes both of the ways above are the same.
calling s.destroy() is better though since you don't use an extra function call.

@Shadowflare
Whenever you create a struct that you are never gonna use again it must be destroyed or you will leak a struct instance.

Ah that makes sense.

I wouldn't actually have such a function, it was just an example to test my question.

It's important to know, because some of my structs require 3-4 array accesses, and it'd be handy just to use a local struct variable and not have to worry about things.

Look at how unreadable this code is...
JASS:
...
if GetTriggeringRegion() == playerDatum[pid].quests[currQuest].goals[currQuestStage].goalRegion then
...

It's three array accesses plus a field method call. I did this because I wasn't sure how local structs behaved.
 
Status
Not open for further replies.
Back
Top