• 🏆 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!

The endless "leak"-thing

Status
Not open for further replies.
Level 3
Joined
Sep 18, 2008
Messages
60
Okay now after a couple of hours speding my time on searching for "does this leak" threads i have a NEW question.

In every thread there are 2 sides:

Group 1: The asking users
Group 2: The "that leaks" users

The process always is the same:

"Does this leak?" - "yes it does .....(posting some functions)" - "*nods* ok"

Maybe my english skills are too bad but i didnt find any "why does something leak if i dont create a varbiable that can leak?"

Let me explain: As a software engineer (C++) i've learned that "leaks" among other things are caused by no longer needed variables that are left in memory.

BUT if you pass the result of a function to another function there is nothing that can leak - because there is no variable.

So please could anyone (maybe a real software engineer) explain why using an objects property in a function (random point in a(function)("region"("name" of an object))) causes a leak???

or do i mix things here?


EDIT: btw - our tutors were some well read guys but no professionals so maybe they've teached some things wrong ;)
 
Level 7
Joined
Jul 20, 2008
Messages
377
I consider myself something of a software engineer.

As you know, GUI is translated into JASS when the map is saved. For example:

  • Unit - Create 1 Footman for Player 1 (Red) at (Random point in (Playable map area)) facing Default building facing (270.0) degrees
Is translated into this:

JASS:
call CreateNUnitsAtLoc( 1, 'hfoo', Player(0), GetRandomLocInRect(GetPlayableMapRect()), bj_UNIT_FACING )

Now, let's look at GetRandomLocInRect(). It looks like this:

JASS:
function GetRandomLocInRect takes rect whichRect returns location
    return Location(GetRandomReal(GetRectMinX(whichRect), GetRectMaxX(whichRect)), GetRandomReal(GetRectMinY(whichRect), GetRectMaxY(whichRect)))
endfunction

Location() creates a location, which is a handle. Handles are like objects in general programming - for an object to be used, it has to be created. Correct?

But here's the catch - how are you going to free the memory used up by the location if you don't store it into a variable? There's no "Last Created Location" function.

Add to this the fact that locations are often nested (i.e. polar offset from a random location). You're creating multiple locations that aren't going to be free-able.

For example:

  • Unit - Create 1 Footman for Player 1 (Red) at (Random point in (Playable map area)) facing Default building facing (270.0) degrees
Creates a location at a random point in the playable map area. That's why to fix this, you would do this:

  • Set tempLoc = (Random point in (Playable map area))
  • Unit - Create 1 Footman for Player 1 (Red) at tempLoc facing Default building facing (270.0) degrees
  • Remove Location - tempLoc
If you're nesting locations, just set each individual location to a variable and have the containing locations use that stored location.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
C++ doesn't have a garbage collector, which means that if you create a pointer, unless you manually delete (C++) or free (C) it, it will stay there until the executable ends.

Since Jass is made from C++, the same rules apply.

Now, warcraft 3 Locations, Groups, Effects, and all those things are pointers.
Here's an example of how they -could- look (since I have no idea how they actually look):

Code:
typedef struct
{
    float x,y;
} *location;

typedef struct
{
    int numOfUnits;
    Vector<unit> units;
} *group;

typedef struct
{
    float x,y;
    char *effect_path;
} *effect;

And like we said before - C++ doesn't automatically remove pointers, so when you call something like "call RemoveLocation(loc)", it's doing something similar to this:
Code:
void RemoveLocation(location l)
{
    delete l; // will work since we declared location as a pointer
}

The only reason I can see to why there are multiple remove functions is that it may change for other types, for example

Code:
void DestroyGroup(group g)
{
    delete [] g->units;
    delete g;
}
 
Last edited:
Level 3
Joined
Sep 18, 2008
Messages
60
Thank you :) another time you've saved my day ;)

but there still is something i dont get... look:

With
  • Unit - Create 1 Footman for Player 1 (Red) at ([B]Random point in (Playable map area[/B])) facing Default building facing (270.0) degrees

"Random Point in (Playable map area)" has to create a location.

but what if we change the "randomized" value to something that is already set. like a "region" named "spawn" or "firstcorner" or something else. Now what i've thought was that "spawn" already is an object and we are refering to its properties like x and y position, width/height and so on. ok ok now while writing "i can see clearly now" :thumbs_up: there still is a location needed to be created IN the coordinates we got from the region object!

that's it!

+rep ;)
 
Level 7
Joined
Jul 20, 2008
Messages
377
Precisely, you'd create a location at the random point in that pre-made location.
 
Level 3
Joined
Sep 18, 2008
Messages
60
C++ doesn't have a garbage collector, which means that if you create a pointer, unless you manually destroy (C++) or free (C) it, it will stay there until the executable ends.

Since Jass is made from C++, the same rules apply.

Now, warcraft 3 Locations, Groups, Effects, and all those things are pointers.
Here's an example of how they -could- look (since I have no idea how they actually look):

Yeah the pointer thing still makes me mad ;) thank you for your detailed description!
 
Status
Not open for further replies.
Top