What-is (and how to avoid) Crashes/Memory-Leaks

svenski

New Member
Reaction score
1
Does anyone have any tips on avoiding game crashes while making your own triggers? I've had a couple of really random game crashes while testing my map without knowing for sure the cause.

Also - what is a memory leak? Just a lost bit of memory that can't be overwritten or re-used?

Two questions on variables:
-Should you always set your local variables = null at the end of your function?

-Do the global variables in the GUI get overwritten when they get set equal to something, or should you always null those before overwriting them? Is it better practice just to always clear your global variables before re-using them to avoid "leaks"?


I've got a spell being used that is allowed to run in multiple instances using local variables, so if I don't clear those variables at the end of the instance, will that cause a memory leak?

Maybe refer me to a guide to read on it, if you know of one.

Thanks for any help : )
 

Viikuna

No Marlo no game.
Reaction score
265
-Should you always set your local variables = null at the end of your function?

No, not always. Only some local handles must be set null. When you have a local variable, which is pointing to some object that gets removed from the game, you must null it. For example, if you destroy an effect and not null local effect variable, you leak. If some unit dies, and you have some local pointing to it, it leaks too. If you null player variables, you do useless job, because players never get removed.
-Do the global variables in the GUI get overwritten when they get set equal to something, or should you always null those before overwriting them? Is it better practice just to always clear your global variables before re-using them to avoid "leaks"?

You dont have to null any globals, no GUI globals nor jass globals or struct members. They get overwriten.

edit. About crashes. There is few things that can crash wc3, you should test all your triggers, one by one, and try to find out which one crashes the game. When you find that one, just post it here, and we can check what exactly is the cause of the crash.

Also, if you dont yet have NewGen get it from here and update newest JasHelper here. NewGen is something you must have, if you do Jass.
 

Viikuna

No Marlo no game.
Reaction score
265
Don't use GetLocalPlayer() unless you know how to use it correctly.

More like this.

edit. You should remember, that there is two kinds of "leaks".
You can leak some object, for example an effect, if you create and effect by using AddSpecialEffect( .. ) and never destroy it by using DestroyEffect( .. ).
You can also leak local handles, when you have a local effect e and you forgot to set e=null after you have destroyed that effect object.
 

Prometheus

Everything is mutable; nothing is sacred
Reaction score
590
I know, I just felt it easier to make them avoid it until they're good enough to figure out that they can use it. :)
 

Viikuna

No Marlo no game.
Reaction score
265
Hm, how can GetLocalPlayer even cause a crash? It can cause desyncs easily, thats for sure, but a crash?
 

Viikuna

No Marlo no game.
Reaction score
265
Yea, desync is bad, but when he said crash, I was more like thinking about some "moving units outside of map bounds" -kind of crash.

Fatal error and shit like that.
 

Tt112233

TH.net Regular
Reaction score
6
Be extra careful when you are using arrays. It causes a fatal error if you for instance, try to retrieve a value from an incorrect index.

Don't use BJ functions. They are completely useless. It makes your code slower.

Instead of using Location variable, it's more useful to use real X and Y coordinates. (Because location leaks easily if not handled properly)
 

saw792

Is known to say things. That is all.
Reaction score
280
Be extra careful when you are using arrays. It causes a fatal error if you for instance, try to retrieve a value from an incorrect index.

Don't use BJ functions. They are completely useless. It makes your code slower.

Instead of using Location variable, it's more useful to use real X and Y coordinates. (Because location leaks easily if not handled properly)

1. No it doesn't. Global arrays are allocated memory so will return null. Local arrays will end the thread for uninitialised values, not crash the game.

2. Generalisation. MOST BJ's are useless.

3. Agreed.
 

svenski

New Member
Reaction score
1
Just to clarify,

Yea, desync is bad, but when he said crash, I was more like thinking about some "moving units outside of map bounds" -kind of crash.

Fatal error and shit like that.

One time i got the common "Fatal Error" crash out of WCIII. But i rewrote that trigger a while ago and it's working fine now.

Another time I got a crash out of WCIII with no explanation, no "Fatal Error" message or anything. The game just turned off. But I rewrote the trigger that I believe caused the problem (using local variables to store units in a trigger with wait commands, whereas before I was trying to add units to a revival-group with the GUI actions, doing a hero-revive sort of command that had to wait until a global timer ran out to revive) and it's been working since.

And thanks for the advice guys, :):):)
 

Tt112233

TH.net Regular
Reaction score
6
1. No it doesn't. Global arrays are allocated memory so will return null. Local arrays will end the thread for uninitialised values, not crash the game.

2. Generalisation. MOST BJ's are useless.

3. Agreed.

1.If you have an User Defined Global Array and try to access an incorrect array index or array index thats not initialized, the game will crash for sure.

2.W/E. 99% of BJ functions are useless there ^^
 

Akolyt0r

New Member
Reaction score
33
there are pretty useful BJs ..examples: .TriggerRegisterAnyUnitEvent, RMinBJ, RMaxBJ, RAbsBJ, IMinBJ, IMaxBJ...
altough some of them should still be inlined when you need lots of performance (>> periodic stuff with tiny timeout)
 

saw792

Is known to say things. That is all.
Reaction score
280
l46kok said:
1.If you have an User Defined Global Array and try to access an incorrect array index or array index thats not initialized, the game will crash for sure.

JASS:
scope Test initializer Init

globals
  private handle array H
  private boolean array B
  private integer array I
  private string array S
  private real array R
endglobals

private function Callback takes nothing returns nothing
  local handle h
  local boolean b
  local integer i
  local string s
  local real r
  
  //! textmacro ARRAY takes A, B, STRING
  set $A$ = $B$[0]
  if $A$ == null then
    call BJDebugMsg("$STRING$ array = null")
  endif
  //! endtextmacro
  
  //! runtextmacro ARRAY("h", "H", "Handle")
  //! runtextmacro ARRAY("b", "B", "Boolean")
  //! runtextmacro ARRAY("i", "I", "Integer")
  //! runtextmacro ARRAY("s", "S", "String")
  //! runtextmacro ARRAY("r", "R", "Real")
endfunction

private function Init takes nothing returns nothing
  local timer t = CreateTimer()
  call TimerStart(t, 0.5, false, function Callback)
endfunction

endscope


Uninitialised global array test. Every single type prints the debug message, thus they are all initialised to null. No crash whatsoever.

JASS:
scope Test initializer Init

private function Callback takes nothing returns nothing
  local handle array H
  local boolean array B
  local integer array I
  local string array S
  local real array R
  
  local handle h
  local boolean b
  local integer i
  local string s
  local real r
  
  //! textmacro ARRAY takes A, B, STRING
  set $A$ = $B$[0]
  if $A$ == null then
    call BJDebugMsg("$STRING$ array = null")
  endif
  //! endtextmacro
  
  //! runtextmacro ARRAY("h", "H", "Handle")
  //! runtextmacro ARRAY("b", "B", "Boolean")
  //! runtextmacro ARRAY("i", "I", "Integer")
  //! runtextmacro ARRAY("s", "S", "String")
  //! runtextmacro ARRAY("r", "R", "Real")
endfunction

private function Init takes nothing returns nothing
  local timer t = CreateTimer()
  call TimerStart(t, 0.5, false, function Callback)
endfunction

endscope


Also prints all debug messages.

JASS:
scope Test initializer Init

private function Callback takes nothing returns nothing
  local handle H
  local boolean B
  local integer I
  local string S
  local real R
  
  local handle h
  local boolean b
  local integer i
  local string s
  local real r
  
  //! textmacro ARRAY takes A, B, STRING
  set $A$ = $B$
  if $A$ == null then
    call BJDebugMsg("$STRING$ array = null")
  endif
  //! endtextmacro
  
  //! runtextmacro ARRAY("h", "H", "Handle")
  //! runtextmacro ARRAY("b", "B", "Boolean")
  //! runtextmacro ARRAY("i", "I", "Integer")
  //! runtextmacro ARRAY("s", "S", "String")
  //! runtextmacro ARRAY("r", "R", "Real")
  call BJDebugMsg("End of Actions")
endfunction

private function Init takes nothing returns nothing
  local timer t = CreateTimer()
  call TimerStart(t, 0.5, false, function Callback)
endfunction

endscope


Prints nothing, as the thread is ended (once again no crash).

JASS:
scope Test initializer Init

globals
  private handle H
  private boolean B
  private integer I
  private string S
  private real R
endglobals

private function Callback takes nothing returns nothing
  local handle h
  local boolean b
  local integer i
  local string s
  local real r
  
  //! textmacro ARRAY takes A, B, STRING
  set $A$ = $B$
  if $A$ == null then
    call BJDebugMsg("$STRING$ array = null")
  endif
  //! endtextmacro
  
  //! runtextmacro ARRAY("h", "H", "Handle")
  //! runtextmacro ARRAY("b", "B", "Boolean")
  //! runtextmacro ARRAY("i", "I", "Integer")
  //! runtextmacro ARRAY("s", "S", "String")
  //! runtextmacro ARRAY("r", "R", "Real")
  call BJDebugMsg("End of Actions")
endfunction

private function Init takes nothing returns nothing
  local timer t = CreateTimer()
  call TimerStart(t, 0.5, false, function Callback)
endfunction

endscope


Prints nothing, thread is ended, no crash.
 

Viikuna

No Marlo no game.
Reaction score
265
Does Player(-1) or some other invalid Player number crash?

( Thats what I have heard, but never tested it )

and ExecuteFunction for some function that doesnt exist might crash too, if I remember correctly.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top