• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] How to check whether all the instances have been deallocated?

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hey ,guys. How to check if all the instances have been deallocated or removed from the linked list in a struct?
I try to check it by calling the following static method:
JASS:
private static method counter takes nothing returns nothing
     local thistype this = thistype(0).next
     loop
          exitwhen this == 0
          call BJDebugMsg(I2S(this.next))
          call BJDebugMsg(I2S(this.prev))
          set this = this.next
     endloop
endmethod

If all the results are zero, then it means all instances have been properly removed from the list, right? If so, how about deallocation?
 
Last edited:
Actually, if all instances have been properly removed from the list, it should display nothing. When you have a list, it doesn't set the next/previous to 0. So if you have this linked list:

(5) - (1) - (2) - (4)

And remove the element (1), you'll be left with:

(5) - (2) - (4)

Therefore, when the list is entirely empty, you'll be left with nothing. thistype(0).next will just be 0. As such, the loop will immediately exit, and nothing will be displayed.

As for deallocation, you can't really check if an instance is deallocated without some of your own work. The internal script of the struct has a way to check that, but you aren't exposed to that variable (I suppose you could read that variable anyway, but it is ugly). I'd do something like this:
JASS:
struct A
    debug boolean allocated // not sure if this will give an error or not

    method destroy takes nothing returns nothing
        // remove element from list and stuff
        debug set this.allocated = false
        call this.deallocate()
    endmethod

    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        debug set this.allocated = true
        return this
    endmethod

endstruct
(this works, assuming .create() is the only way to allocate an instance, and .destroy() is the only way to deallocate one)

Then, if you want to print which instances are allocated, you could just do something like this:
JASS:
static method printActiveInstances takes nothing returns nothing
    local integer i = 0
    loop
         exitwhen i == 8191 // wasteful, but it is for debugging anyway
         if A(i).allocated then // check if the instance corresponding with the integer 'i' is allocated
             call BJDebugMsg("(" + I2S(i) + ")") // print it, e.g. (1), (2), (3) etc..
         endif
         set i = i + 1
    endloop
endmethod
 
Level 11
Joined
Oct 11, 2012
Messages
711
Actually, if all instances have been properly removed from the list, it should display nothing. When you have a list, it doesn't set the next/previous to 0. So if you have this linked list:

(5) - (1) - (2) - (4)

And remove the element (1), you'll be left with:

(5) - (2) - (4)

Therefore, when the list is entirely empty, you'll be left with nothing. thistype(0).next will just be 0. As such, the loop will immediately exit, and nothing will be displayed.

As for deallocation, you can't really check if an instance is deallocated without some of your own work. The internal script of the struct has a way to check that, but you aren't exposed to that variable (I suppose you could read that variable anyway, but it is ugly). I'd do something like this:
JASS:
struct A
    debug boolean allocated // not sure if this will give an error or not

    method destroy takes nothing returns nothing
        // remove element from list and stuff
        debug set this.allocated = false
        call this.deallocate()
    endmethod

    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        debug set this.allocated = true
        return this
    endmethod

endstruct
(this works, assuming .create() is the only way to allocate an instance, and .destroy() is the only way to deallocate one)

Then, if you want to print which instances are allocated, you could just do something like this:
JASS:
static method printActiveInstances takes nothing returns nothing
    local integer i = 0
    loop
         exitwhen i == 8191 // wasteful, but it is for debugging anyway
         if A(i).allocated then // check if the instance corresponding with the integer 'i' is allocated
             call BJDebugMsg("(" + I2S(i) + ")") // print it, e.g. (1), (2), (3) etc..
         endif
         set i = i + 1
    endloop
endmethod

Thanks, PnF. I tried your method but an error message popped out: "allocated is not a member of A". :goblin_cry:

Edit:
I removed "debug" in front of "boolean allocated" and no error pops out.
Now it keeps showing "(1)" and "(2)". I have two units casting the spell at the same time for the purpose of the test. So does this mean that I am deallocating correctly?
 
Last edited:
Level 11
Joined
Oct 11, 2012
Messages
711
Is debug mode enabled (under the Grimoire tab, or JassHelper, I forget which)?

Could you show your code for your spell? If you deallocated everything and there are no active instances, then it shouldn't display anything. It should only display (1) (2) if the spell's code is still running for that instance.

I found the problem and fixed it and it didn't show anything now. Thank you so much, PnF.
http://www.hiveworkshop.com/forums/...ng-units-height-vjass-spell-need-help-252309/ Here is the code. Can you take a look at it if you have time? It works as intended but I am wondering if there is any leak or other errors. :) Thanks again.

Edit: I think it still bugs if two units cast the spell at the same time..... :(
 
I found the problem and fixed it and it didn't show anything now. Thank you so much, PnF.
http://www.hiveworkshop.com/forums/...ng-units-height-vjass-spell-need-help-252309/ Here is the code. Can you take a look at it if you have time? It works as intended but I am wondering if there is any leak or other errors. :) Thanks again.

Edit: I think it still bugs if two units cast the spell at the same time..... :(

Yeah, I know what issue is. It is just going to be a long post. I'll try to make it as soon as I can though, lol.
 
Level 11
Joined
Oct 11, 2012
Messages
711
Yeah, I know what issue is. It is just going to be a long post. I'll try to make it as soon as I can though, lol.

I really appreciate that, PnF. Thanks in advance for your time :ogre_love:
In case the following information can help: when only one unit casts it, no message shows up. However, if two units cast it at the same time for a period of time, it shows like the following: (2)(1)(2)(1)(2)(1)...... BIG THANKS!
 
Status
Not open for further replies.
Top