• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] Question for a PRO :P

Status
Not open for further replies.
Level 11
Joined
Dec 31, 2007
Messages
780
lets start by the beginning

(**skip this.... its just how i got up to here**)

im starting to study at the university how to program... last week we saw pointers, and a lot of things came up to my mind.

(**up to here**)

thinking a bit about leaks, jass, calls and thingys these questions came to me:

Points are pointers. arent they?

pointers needs to be removed for the information that they are pointing to might be removed from memory (else lag and things will happen bla bla bla)

i heard that if you replace the information stored in a pointer for new information (not another variable) you dont need to remove the pointer coz the link is gonna be erased anyways.

this made me think about locations

why should we remove locations when giving them a new value?

is that the new values we are giving them (for ie. Position of unit) are pointers?

in the position of unit i mentioned. Does it call a function that returns a pointer and assigns it to the point variable? and that is the reason for us to remove the location variable before re-assigning it?

if WE works with functions. Why pointers are not deleted automatically after the function has ended?

may this be the reason for a lot of people complaining about WE not having something about a "garbage collector" or sort of that?

Side note: i know nothing (except for some function calls) about jass, i read absolutely nothing about jass... all this came to me when i understood a bit about dinamic memory

thy for reading ^^
 
Level 11
Joined
Feb 22, 2006
Messages
752
JASS does not have a garbage collector (the only exception here is with function parameters: those DO get garbage-collected when the function ends), so you have to manually null all handle variables (variables of all types except integer, real, boolean, string, and code).

In JASS, every handle is actually just an integer (which is why you can cast handles to integers with H2I), so handle variables do nothing but reference a reference (the integer, called the handle ID) to the actual handle objects. However, in-game handles are actually treated as objects, so they take up more memory than the 32-bits the handle variables require (I think variables are 32-bit integers...don't quote me on this tho). Therefore, you need to also manually destroy/remove handles when you are done with them by calling the appropriate natives (like RemoveLocation). In the end destroying/removing handles is more important than nulling the handle variables.

As for how GetUnitLoc() works, it creates a Location handle and gives it the appropriate info (where the unit is located on the map), then returns a handle ID of type Location that allows JASS script to "manipulate" that Location handle. The Location handle will stick around in memory forever until the game ends or RemoveLocation() is called on it.

For future reference, you shouldn't try to apply too much about what you learn in CS classes to JASS. JASS is simply a scripting syntax, so it lacks many features that programming languages (C++, Java, Python, etc.) have.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
There is no reason warcraft will remove your objects alone, since it would require it to track everything and work much more just for this.

As to why a new location is made every time you set values - say thanks to Blizzard for not adding SetX/Y/Position methods to the location struct/class.
Since they didn't make those, when you declare a location it simply makes your pointer to point to a new location object with your new x/y offsets.

Since C++ (which Jass wraps) doesn't know how to remove custom types (which makes sense), you need to tell it how to remove it, which is what the Destroy/Remove functions do.

I'm also pretty sure warcraft uses 2 byte integers for IDs but it really doesn't matter :p
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
why should we remove locations when giving them a new value?
Because you're changing the value of the pointer instead of the values of where the pointer is pointing at. You change the adress (where the pointer points at) to another location object, leaving the old location in the memory. Since you no longer have a pointer pointing to the old location, it'll stay in memory forever (because you can't remove it, because you have no pointer pointing to it), and it leaks.

if WE works with functions. Why pointers are not deleted automatically after the function has ended?
The pointers are deleted. The objects that were referred to BY the pointers are not. That's pretty good actually:
If you would make a new unit with the CreateUnit() function (which returns a pointer to a unit object), the local unit pointer variable is removed after ending the function (thus, the pointer is gone), but the object itself is still in memory. Otherwise, the unit would be removed from the game after ending the function, which is not what we want, right?

You must think of a pointer as a simple integer. It's an integer that contains the adress of an object. An object is a collection of variables (i.e. a "unit" object has a real hp, real mana, real cooldown, string name, etc.).
When you use a "unit" variable in jass, you're using a pointer. It's simply an integer containing an adress.
When you call a function (for example: SetUnitState), you give the "unit" pointer as a parameter. Internally, it'll then look up the object BEHIND the "unit" pointer and change its life or mana or whatever.
When a function with a local unit variable is terminated, the local unit variable (which is a pointer, i.e. an integer containing an adress) which uses 4 bytes of memory, will be removed (and 4 bytes of memory are freed). However, the object where the pointer was pointing at is still in memory, obviously.

In warcraft 3, things are a bit more complex because we're talking about handles, not pointers...

may this be the reason for a lot of people complaining about WE not having something about a "garbage collector" or sort of that?
Thinking of garbage collection, it's probably better not to have it in a game, because a game like warcraft (or starcraft 2) really needs to run as efficient as possible...

Since C++ (which Jass wraps) doesn't know how to remove custom types (which makes sense), you need to tell it how to remove it, which is what the Destroy/Remove functions do.
According to the c++ standard, compilers should automatically create default constructos / destructors...
Jass too knows perfectly how to remove objects (RemoveUnit, RemoveLocation, ...), it just doesn't know WHEN to do so.

I'm also pretty sure warcraft uses 2 byte integers for IDs but it really doesn't matter :p
I'm pretty sure everything is in 4 bytes in warcraft.
 
Status
Not open for further replies.
Top