• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[AI] Trying to plug some leaks

Status
Not open for further replies.
Level 10
Joined
Mar 31, 2009
Messages
732
I've got some leaky code here, in functions that I use in a few places in my map.
I'm not sure how to plug the leaks though. Some kind of handle wrapper? Or is there a simpler way I've missed?


JASS:
library filters
    globals
        group enumGrp = CreateGroup()
    endglobals

    function TrueFilter takes nothing returns boolean
        return true
    endfunction
    
    function Get_unit_in_square takes real x, real y returns unit
        local unit u
        call GroupEnumUnitsInRange(enumGrp, x, y, 200, Filter(function TrueFilter))
        set u = FirstOfGroup(enumGrp)
        call GroupClear(enumGrp)
        return u
        // leeeeeeeeeeeeeeeak
    endfunction
    
    function get_random_unit_of_player takes integer playerID returns unit
        local unit u
        call GroupEnumUnitsOfPlayer(enumGrp, Player(playerID), null)
        set u = GroupPickRandomUnit(enumGrp)
        call GroupClear(enumGrp)
        return u //leeeeak
    endfunction
    
    function get_random_unit_of_type takes integer unitid, integer playerNum returns unit
        local unit u
        set bj_groupEnumTypeId = unitid
        call GroupEnumUnitsOfPlayer(enumGrp, Player(playerNum), filterGetUnitsOfPlayerAndTypeId)
        set u = GroupPickRandomUnit(enumGrp)
        call GroupClear(enumGrp)
        return u //leeeeeak
    endfunction
endlibrary
 
Level 8
Joined
Feb 15, 2009
Messages
463

JASS:
library filters 
    globals
        group enumGrp = CreateGroup()
    endglobals


//why return always true ? just for test i think

    function TrueFilter takes nothing returns boolean
        return true
    endfunction
    
// dont understand why u have the GetUnitInSquare function ( for what u need it)

    function Get_unit_in_square takes real x, real y returns unit
        local unit u
        
        local boolexpr b = Condition( function TrueFilter )

        call GroupEnumUnitsInRange(enumGrp, x, y, 200, b)

        set u = FirstOfGroup(enumGrp)

        call GroupClear(enumGrp)
        call DestroyBoolExpr ( b)
        set b = null
        return u    
    endfunction
    
    function get_random_unit_of_player takes integer playerID returns unit
        local unit u
        call GroupEnumUnitsOfPlayer(enumGrp, Player(playerID), null)
        set u = GroupPickRandomUnit(enumGrp)
        call GroupClear(enumGrp)
        return u 
//leeeeak
//u cant remove that player or unit coz then u have nothing =D
    endfunction
    
    function get_random_unit_of_type takes integer unitid, integer playerNum returns unit
        local unit u
        set bj_groupEnumTypeId = unitid
        call GroupEnumUnitsOfPlayer(enumGrp, Player(playerNum), filterGetUnitsOfPlayerAndTypeId) 
// dunno where this function is but a make local boolexpr like in first example
        set u = GroupPickRandomUnit(enumGrp)
        call GroupClear(enumGrp)
        return u //leeeeeak
    endfunction
endlibrary



ALso your function naming method is very bad ( capslock etc. ....) rather go for things like this:
1.Yours
get_random_unit_of_type
2. Better
GetRandomUnitOfType
 
Level 10
Joined
Mar 31, 2009
Messages
732
//why return always true ? just for test i think
Its code I got from some other poster, when I wasn't sure how to stop using the BJ's.

// dont understand why u have the GetUnitInSquare function ( for what u need it)
Making a chess board map. Only one unit in a square, always placed on the center. That function is to return the unit in the square with coordinates x,y.

//u cant remove that player or unit coz then u have nothing =D
I didn't null the local variable?

// dunno where this function is but a make local boolexpr like in first example
Its a BJ function. Copied most of that from one of the functions in blizzard.j.

ALso your function naming method is very bad ( capslock etc. ....) rather go for things like this:
Meh. Function name is a function name. Least I know it was a function I made myself, If I change them I'd probably opt for java-type names: getTargetOfTarget(); getCodeToDestroyTheWorld(worlds.getWorld(3));
 
Level 8
Joined
Feb 15, 2009
Messages
463
Ok ok

Its code I got from some other poster, when I wasn't sure how to stop using the BJ's.Just make a filter with what u want and i showed you that you have a leak there and yo can clean it with the boolexpr thing i showed you

Making a chess board map. Only one unit in a square, always placed on the center. That function is to return the unit in the square with coordinates x,y.
OK
I didn't null the local variable?
The thing i want to say there , that there is no leak to clean (it leaks[a bit] but is not avoidable)


Its a BJ function. Copied most of that from one of the functions in blizzard.j.
Aha ok


Meh. Function name is a function name. Least I know it was a function I made myself, If I change them I'd probably opt for java-type names: getTargetOfTarget(); getCodeToDestroyTheWorld(worlds.getWorld(3));
Just wanted to show you
 
Level 10
Joined
Mar 31, 2009
Messages
732
The truefilter is there because passing "null" as a filter in GroupEnumUnits would cause a leak. Don't ask me why, but apparently it does.

I also don't think the first post really leaks...

I thought that not nulling the locals leaks?

I just went with the global idea. Figured I could easily define it in that libraries global block, and all would work out well.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
I thought that not nulling the locals leaks?

Not that I know how Jass works internally, but the fact that you return the unit seems to be the exact reason why it wouldn't leak, as you return the pointer to the calling function.



but i didnt ment that he should clean the filter but the last argument of the GroupEnumUnitsInRange function which is a boolexpr so he has to go for
local boolexpr b = Condition( function ... )

call DestroyBoolexpr(b)
set b= null

That's like saying "he doesn't leak, but lets create a leak just so we could destroy it every time this trigger runs! yey!".
 
Level 10
Joined
Mar 31, 2009
Messages
732
Nulling does not solve leaks. It solves a reference bug which, since you're returning the created unit, does not occur

Ok so there is nothing wrong with doing this?

JASS:
    function Get_unit_in_square takes real x, real y returns unit
        call GroupEnumUnitsInRange(enumGrp, x, y, 200, Filter(function TrueFilter))
        return FirstOfGroup(enumGrp)
    endfunction
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Nothing wrong with that.

You might need to clear the group first though, to avoid bugs.
JASS:
    function Get_unit_in_square takes real x, real y returns unit
        call GroupClear(enumGrp)
        call GroupEnumUnitsInRange(enumGrp, x, y, 200, Filter(function TrueFilter))
        return FirstOfGroup(enumGrp)
    endfunction
 
Status
Not open for further replies.
Top