Objects

An object in AutoHotkey is an abstract datatype which provides three basic functions:

Related topics:

IsObject() can be used to determine if a value is an object:

Result := IsObject(expression)

See Object Types in the documentation side bar for a list of standard object types. There are three fundamental types:

Support for objects requires [AHK_L 31+], but some features may require a later version.

Table of Contents

Basic Usage

Simple Arrays [v1.1.21+]

Create an array:

Array := [Item1, Item2, ..., ItemN]
Array := Array(Item1, Item2, ..., ItemN)

Retrieve an item:

Value := Array[Index]

Assign an item:

Array[Index] := Value

Insert one or more items at a given index using the InsertAt method:

Array.InsertAt(Index, Value, Value2, ...)

Append one or more items using the Push method:

Array.Push(Value, Value2, ...)

Remove an item using the RemoveAt method:

RemovedValue := Array.RemoveAt(Index)

Remove the last item using the Pop method:

RemovedValue := Array.Pop()

If the array is not empty, MinIndex and MaxIndex/Length return the lowest and highest index currently in use in the array. Since the lowest index is nearly always 1, MaxIndex usually returns the number of items. However, if there are no integer keys, MaxIndex returns an empty string whereas Length returns 0. Looping through an array's contents can be done either by index or with a For-loop. For example:

array := ["one", "two", "three"]

; Iterate from 1 to the end of the array:
Loop % array.Length()
    MsgBox % array[A_Index]

; Enumerate the array's contents:
For index, value in array
    MsgBox % "Item " index " is '" value "'"

Associative Arrays [v1.1.21+]

An associative array is an object which contains a collection of unique keys and a collection of values, where each key is associated with one value. Keys can be strings, integers or objects, while values can be of any type. An associative array can be created as follows:

Array := {KeyA: ValueA, KeyB: ValueB, ..., KeyZ: ValueZ}
Array := Object("KeyA", ValueA, "KeyB", ValueB, ..., "KeyZ", ValueZ)

Using the {key:value} notation, quote marks are optional for keys which consist only of word characters. Any expression can be used as a key, but to use a variable as a key, it must be enclosed in parentheses. For example, {(KeyVar): Value} and {GetKey(): Value} are both valid.

Retrieve an item:

Value := Array[Key]

Assign an item:

Array[Key] := Value

Remove an item using the Delete method:

RemovedValue := Array.Delete(Key)

Enumerating items:

array := {ten: 10, twenty: 20, thirty: 30}
For key, value in array
    MsgBox %key% = %value%

Associative arrays can be sparsely populated - that is, {1:"a",1000:"b"} contains only two key-value pairs, not 1000.

In AutoHotkey v1.x, simple arrays and associative arrays are the same thing. However, treating [] as a simple linear array helps to keep its role clear, and improves the chance of your script working with a future version of AutoHotkey, which might differentiate between simple arrays and associative arrays.

Objects [AHK_L 31+]

For all types of objects, the notation Object.LiteralKey can be used to access a property, array element or method, where LiteralKey is an identifier or integer and Object is any expression. Identifiers are unquoted strings which may consist of alphanumeric characters, underscore and, in [v1.1.09+], non-ASCII characters. For example, match.Pos is equivalent to match["Pos"] while arr.1 is equivalent to arr[1]. There must be no space after the dot.

Examples:

Retrieve a property:

Value := Object.Property

Set a property:

Object.Property := Value

Call a method:

ReturnValue := Object.Method(Parameters)

Call a method with a computed method name:

ReturnValue := Object[MethodName](Parameters)

Some properties of COM objects and user-defined objects can accept parameters:

Value := Object.Property[Parameters]
Object.Property[Parameters] := Value

Related: Object, File Object, Func Object, COM object

Known limitation:

Freeing Objects

Scripts do not free objects explicitly. When the last reference to an object is released, the object is freed automatically. A reference stored in a variable is released automatically when that variable is assigned some other value. For example:

obj := {}  ; Creates an object.
obj := ""  ; Releases the last reference, and therefore frees the object.

Similarly, a reference stored in a field of another object is released when that field is assigned some other value or removed from the object. This also applies to arrays, which are actually objects.

arr := [{}]  ; Creates an array containing an object.
arr[1] := {}  ; Creates a second object, implicitly freeing the first object.
arr.RemoveAt(1)  ; Removes and frees the second object.

Because all references to an object must be released before the object can be freed, objects containing circular references aren't freed automatically. For instance, if x.child refers to y and y.parent refers to x, clearing x and y is not sufficient since the parent object still contains a reference to the child and vice versa. To resolve this situation, remove the circular reference.

x := {}, y := {}             ; Create two objects.
x.child := y, y.parent := x  ; Create a circular reference.

y.parent := ""               ; The circular reference must be removed before the objects can be freed.
x := "", y := ""             ; Without the above line, this would not free the objects.

For more advanced usage and details, see Reference Counting.

Remarks

Syntax

All types of objects support both array syntax (brackets) and object syntax (dots).

Additionally, object references can themselves be used in expressions:

If an object is used in any context where an object is not expected, it is treated as an empty string. For example, MsgBox %object% shows an empty MsgBox and object + 1 yields an empty string. Do not rely on this behaviour as it may change.

When a method-call is followed immediately by an assignment operator, it is equivalent to setting a property with parameters. For example, the following are equivalent:

obj.item(x) := y
obj.item[x] := y

Compound assignments such as x.y += 1 and --arr[1] are supported.

[v1.1.20+]: Parameters can be omitted when getting or setting properties. For example, x[,2]. Scripts can utilize this by defining default values for parameters in properties and meta-functions. The method name can also be completely omitted, as in x[](a). Scripts can utilize this by defining a default value for the __Call meta-function's first parameter, since it is not otherwise supplied with a value. Note that this differs from x.(a), which is equivalent to x[""](a). If the property or method name is omitted when invoking a COM object, its "default member" is invoked.

Keys

Some limitations apply to which values can be used as keys in objects created with [], {} or the new operator:

Extended Usage

Function References [v1.1.00+]

If the variable func contains a function name, the function can be called one of two ways: %func%() or func.(). However, this requires the function name to be resolved each time, which is inefficient if the function is called more than once. To improve performance, the script can retrieve a reference to the function and store it for later use:

Func := Func("MyFunc")

A function can be called by reference using the following syntax:

RetVal := %Func%(Params)     ; Requires [v1.1.07+]
RetVal := Func.Call(Params)  ; Requires [v1.1.19+]
RetVal := Func.(Params)      ; Not recommended

For details about additional properties of function references, see Func Object.

Arrays of Arrays

AutoHotkey supports "multi-dimensional" arrays by transparently storing arrays inside other arrays. For example, a table could be represented as an array of rows, where each row is itself an array of columns. In that case, the content of column y of row x can be set using either of the methods below:

table[x][y] := content  ; A
table[x, y] := content  ; B

If table[x] does not exist, A and B differ in two ways:

Multi-dimensional assignments such as table[a, b, c, d] := value are handled as follows:

This behaviour only applies to script-created objects, not more specialized types of objects such as COM objects or COM arrays.

Arrays of Functions

An array of functions is simply an array containing function names or references. For example:

array := [Func("FirstFunc"), Func("SecondFunc")]

; Call each function, passing "foo" as a parameter:
Loop 2
    array[A_Index].Call("foo")

; Call each function, implicitly passing the array itself as a parameter:
Loop 2
    array[A_Index]()

FirstFunc(param) {
    MsgBox % A_ThisFunc ": " (IsObject(param) ? "object" : param)
}
SecondFunc(param) {
    MsgBox % A_ThisFunc ": " (IsObject(param) ? "object" : param)
}

For backward-compatibility, the second form will not pass array as a parameter if array[A_Index] contains a function name instead of a function reference. However, if array[A_Index] is inherited from array.base[A_Index], array will be passed as a parameter.

Custom Objects

Objects created by the script do not need to have any predefined structure. Instead, each object can inherit properties and methods from its base object (otherwise known as a "prototype" or "class"). Properties and methods can also be added to (or removed from) an object at any time, and those changes will affect any and all derived objects. For more complex or specialized situations, a base object can override the standard behaviour of any objects derived from it by defining meta-functions.

Base objects are just ordinary objects, and are typically created one of two ways:

class baseObject {
    static foo := "bar"
}
; OR
baseObject := {foo: "bar"}

To create an object derived from another object, scripts can assign to the base property or use the new keyword:

obj1 := Object(), obj1.base := baseObject
obj2 := {base: baseObject}
obj3 := new baseObject
MsgBox % obj1.foo " " obj2.foo " " obj3.foo

It is possible to reassign an object's base at any time, effectively replacing all of the properties and methods that the object inherits.

Prototypes

Prototype or base objects are constructed and manipulated the same as any other object. For example, an ordinary object with one property and one method might be constructed like this:

; Create an object.
thing := {}
; Store a value.
thing.foo := "bar"
; Create a method by storing a function reference.
thing.test := Func("thing_test")
; Call the method.
thing.test()

thing_test(this) {
    MsgBox % this.foo
}

When thing.test() is called, thing is automatically inserted at the beginning of the parameter list. However, for backward-compatibility, this does not occur when a function is stored by name (rather than by reference) directly in the object (rather than being inherited from a base object). By convention, the function is named by combining the "type" of object and the method name.

An object is a prototype or base if another object derives from it:

other := {}
other.base := thing
other.test()

In this case, other inherits foo and test from thing. This inheritance is dynamic, so if thing.foo is modified, the change will be reflected by other.foo. If the script assigns to other.foo, the value is stored in other and any further changes to thing.foo will have no effect on other.foo. When other.test() is called, its this parameter contains a reference to other instead of thing.

Classes [v1.1.00+]

At its root, a "class" is a set or category of things having some property or attribute in common. Since a base or prototype object defines properties and behaviour for set of objects, it can also be called a class object. For convenience, base objects can be defined using the "class" keyword as shown below:

class ClassName extends BaseClassName
{
    InstanceVar := Expression
    static ClassVar := Expression

    class NestedClass
    {
        ...
    }

    Method()
    {
        ...
    }

    Property[]  ; Brackets are optional
    {
        get {
            return ...
        }
        set {
            return ... := value
        }
    }
}

When the script is loaded, this constructs an object and stores it in the global (or in [v1.1.05+] super-global) variable ClassName. To reference this class inside a force-local function (or an assume-local or assume-static function prior to [v1.1.05]), a declaration such as global ClassName is required. If extends BaseClassName is present, BaseClassName must be the full name of another class (but as of [v1.1.11], the order that they are defined in does not matter). The full name of each class is stored in object.__Class.

Because the class is referenced via a variable, the class name cannot be used to both reference the class and create a separate variable (such as to hold an instance of the class) in the same context. For example, box := new Box would replace the class object in Box with an instance of itself. [v1.1.27+]: #Warn ClassOverwrite enables a warning to be shown at load time for each attempt to overwrite a class.

Within this documentation, the word "class" on its own usually means a class object constructed with the class keyword.

Class definitions can contain variable declarations, method definitions and nested class definitions.

Instance Variables [v1.1.01+]

An instance variable is one that each instance of the class (that is, each object derived from the class) has its own copy of. They are declared like normal assignments, but the this. prefix is omitted (only directly within the class body):

InstanceVar := Expression

These declarations are evaluated each time a new instance of the class is created with the new keyword. The method name __Init is reserved for this purpose, and should not be used by the script. The __New() method is called after all such declarations have been evaluated, including those defined in base classes. Expression can access other instance variables and methods via this, but all other variable references are assumed to be global.

To access an instance variable (even within a method), always specify the target object; for example, this.InstanceVar.

[v1.1.08+]: Declarations like x.y := z are also supported, provided that x was previously declared in this class. For example, x := {}, x.y := 42 declares x and also initializes this.x.y.

Static/Class Variables [v1.1.00.01+]

Static/class variables belong to the class itself, but can be inherited by derived objects (including subclasses). They are declared like instance variables, but using the static keyword:

static ClassVar := Expression

Static declarations are evaluated only once, before the auto-execute section, in the order they appear in the script. Each declaration stores a value in the class object. Any variable references in Expression are assumed to be global.

To assign to a class variable, always specify the class object; for example, ClassName.ClassVar := Value. If an object x is derived from ClassName and x itself does not contain the key "ClassVar", x.ClassVar may also be used to dynamically retrieve the value of ClassName.ClassVar. However, x.ClassVar := y would store the value in x, not in ClassName.

[v1.1.08+]: Declarations like static x.y := z are also supported, provided that x was previously declared in this class. For example, static x := {}, x.y := 42 declares x and also initializes ClassName.x.y.

Nested Classes

Nested class definitions allow a class object to be stored inside another class object rather than a separate global variable. In the example above, class NestedClass constructs an object and stores it in ClassName.NestedClass. Subclasses could inherit NestedClass or override it with their own nested class (in which case new this.NestedClass could be used to instantiate whichever class is appropriate).

class NestedClass
{
    ...
}

Methods

Method definitions look identical to function definitions. Each method has a hidden parameter named this, which typically contains a reference to an object derived from the class. However, it could contain a reference to the class itself or a derived class, depending on how the method was called. Methods are stored by reference in the class object.

Method()
{
    ...
}

Inside a method, the pseudo-keyword base can be used to access the super-class versions of methods or properties which are overridden in a derived class. For example, base.Method() in the class defined above would call the version of Method which is defined by BaseClassName. Meta-functions are not called; otherwise, base.Method() behaves like BaseClassName.Method.Call(this). That is,

base only has special meaning if followed by a dot . or brackets [], so code like obj := base, obj.Method() will not work. Scripts can disable the special behaviour of base by assigning it a non-empty value; however, this is not recommended. Since the variable base must be empty, performance may be reduced if the script omits #NoEnv.

Properties [v1.1.16+]

Property definitions allow a method to be executed whenever the script gets or sets a specific key.

Property[]
{
    get {
        return ...
    }
    set {
        return ... := value
    }
}

Property is simply the name of the property, which will be used to invoke it. For example, obj.Property would call get while obj.Property := value would call set. Within get or set, this refers to the object being invoked. Within set, value contains the value being assigned.

Parameters can be passed by enclosing them in square brackets to the right of the property name, both when defining the property and when calling it. Aside from using square brackets, parameters of properties are defined the same way as parameters of methods - optional, ByRef and variadic parameters are supported.

The return value of get or set becomes the result of the sub-expression which invoked the property. For example, val := obj.Property := 42 stores the return value of set in val.

Each class can define one or both halves of a property. If a class overrides a property, it can use base.Property to access the property defined by its base class. If get or set is not defined, it can be handled by a base class. If set is not defined and is not handled by a meta-function or base class, assigning a value stores it in the object, effectively disabling the property.

Internally, get and set are two separate methods, so cannot share variables (except by storing them in this).

Meta-functions provide a broader way of controlling access to properties and methods of an object, but are more complicated and error-prone.

Construction and Destruction

Whenever a derived object is created with the new keyword in [v1.1.00+], the __New method defined by its base object is called. This method can accept parameters, initialize the object and override the result of the new operator by returning a value. When an object is destroyed, __Delete is called. For example:

m1 := new GMem(0, 20)
m2 := {base: GMem}.__New(0, 30)

class GMem
{
    __New(aFlags, aSize)
    {
        this.ptr := DllCall("GlobalAlloc", "UInt", aFlags, "Ptr", aSize, "Ptr")
        if !this.ptr
            return ""
        MsgBox % "New GMem of " aSize " bytes at address " this.ptr "."
        return this  ; This line can be omitted when using the 'new' operator.
    }

    __Delete()
    {
        MsgBox % "Delete GMem at address " this.ptr "."
        DllCall("GlobalFree", "Ptr", this.ptr)
    }
}

__Delete is not called for any object which has the key "__Class". Class objects have this key by default.

If the class has a super-class which defines these methods, base.__New() (with parameters as appropriate) and base.__Delete() should typically be called. Otherwise, only the most derived definition of the method is called, excluding any definition within the target object itself.

[v1.1.28+]: If an exception or runtime error is thrown while __Delete is executing and is not handled within __Delete, it acts as though __Delete was called from a new thread. That is, an error dialog is displayed and __Delete returns, but the thread does not exit (unless it was already exiting). Prior to v1.1.28, unhandled exceptions caused inconsistent behavior.

If the script is directly terminated by any means, including the tray menu, ExitApp, or Exit (when the script is not persistent), any functions which have yet to return do not get the chance to do so. Therefore, any objects referenced by local variables of those functions are not released, so __Delete is not called.

When the script exits, objects contained by global and static variables are released automatically in an arbitrary, implementation-defined order. When __Delete is called during this process, some global or static variables may have already been released, but any references contained by the object itself are still valid. It is therefore best for __Delete to be entirely self-contained, and not rely on any global or static variables.

Meta-Functions

Method syntax:
class ClassName {
    __Get([Key, Key2, ...])
    __Set([Key, Key2, ...], Value)
    __Call(Name [, Params...])
}

Function syntax:
MyGet(this [, Key, Key2, ...])
MySet(this [, Key, Key2, ...], Value)
MyCall(this, Name [, Params...])

ClassName := { __Get: Func("MyGet"), __Set: Func("MySet"), __Call: Func("MyCall") }

Meta-functions define what happens when a key is requested but not found within the target object. For example, if obj.key has not been assigned a value, it invokes the __Get meta-function. Similarly, obj.key := value invokes __Set and obj.key() invokes __Call. These meta-functions (or methods) would need to be defined in obj.base, obj.base.base or such.

Meta-functions are generally defined like methods, but do not follow the same rules (except when called explicitly by the script). They must be defined in a base object; any definition in the target object itself is ignored. Each definition of __Get, __Set and __Call applicable to the target object is called automatically according to the rules below, and should not call base.__Get(key) or similar. __New and __Delete must be defined in a base object, but otherwise behave like methods.

Note: AutoHotkey v2 replaces meta-functions with more conventional methods.

When the script gets, sets or calls a key which does not exist within the target object, the base object is invoked as follows:

If a meta-function stores a matching key in the object but does not return, the behaviour is the same as if the key initially existed in the object. For an example using __Set, see Subclassing Arrays of Arrays.

If the operation still hasn't been handled, check if this is a built-in method or property:

If the operation still hasn't been handled,

Known limitation:

Dynamic Properties

Property syntax can be used to define properties which compute a value each time they are evaluated, but each property must be known in advance and defined individually in the script. By contrast, __Get and __Set can be used to implement properties which aren't known by the script.

For example, a "proxy" object could be created which sends requests for properties over the network (or through some other channel). A remote server would send back a response containing the value of the property, and the proxy would return the value to its caller. Even if the name of each property was known in advance, it would not be logical to define each property individually in the proxy class since every property does the same thing (send a network request). Meta-functions receive the property name as a parameter, so are a good solution to this problem.

Another use of __Get and __Set is to implement a set of related properties which share code. In the example below they are used to implement a "Color" object with R, G, B and RGB properties, where only the RGB value is actually stored:

red  := new Color(0xff0000), red.R -= 5
cyan := new Color(0), cyan.G := 255, cyan.B := 255

MsgBox % "red: " red.R "," red.G "," red.B " = " red.RGB
MsgBox % "cyan: " cyan.R "," cyan.G "," cyan.B " = " cyan.RGB

class Color
{
    __New(aRGB)
    {
        this.RGB := aRGB
    }

    static Shift := {R:16, G:8, B:0}

    __Get(aName)
    {
        ; NOTE: Using this.Shift here would cause an infinite loop!
        shift := Color.Shift[aName]  ; Get the number of bits to shift.
        if (shift != "")  ; Is it a known property?
            return (this.RGB >> shift) & 0xff
        ; NOTE: Using 'return' here would break this.RGB.
    }

    __Set(aName, aValue)
    {
        if ((shift := Color.Shift[aName]) != "")
        {
            aValue &= 255  ; Truncate it to the proper range.

            ; Calculate and store the new RGB value.
            this.RGB := (aValue << shift) | (this.RGB & ~(0xff << shift))

            ; 'Return' must be used to indicate a new key-value pair should not be created.
            ; This also defines what will be stored in the 'x' in 'x := clr[name] := val':
            return aValue
        }
        ; NOTE: Using 'return' here would break this.stored_RGB and this.RGB.
    }

    ; Meta-functions can be mixed with properties:
    RGB {
        get {
            ; Return it in hex format:
            return format("0x{:06x}", this.stored_RGB)
        }
        set {
            return this.stored_RGB := value
        }
    }
}

However, in this case Property syntax could have been used instead, where code is shared by simply having each property call a central method. It is better to avoid using meta-functions where possible due to the high risk of misuse (see the notes in red above).

Objects as Functions

For an outline of how to create objects which can act as functions, see Function Objects.

A function object can also act as a meta-function, such as to define dynamic properties similar to those in the previous section. Although it is recommended to use property syntax instead, the example below shows the potential of meta-functions for implementing new concepts or behaviour, or changing the structure of the script.

; This example requires the FunctionObject class in order to work.
blue := new Color(0x0000ff)
MsgBox % blue.R "," blue.G "," blue.B

class Properties extends FunctionObject
{
    Call(aTarget, aName, aParams*)
    {
        ; If this Properties object contains a definition for this half-property, call it.
        if ObjHasKey(this, aName)
            return this[aName].Call(aTarget, aParams*)
    }
}

class Color
{
    __New(aRGB)
    {
        this.RGB := aRGB
    }

    class __Get extends Properties
    {
        R() {
            return (this.RGB >> 16) & 255
        }
        G() {
            return (this.RGB >> 8) & 255
        }
        B() {
            return this.RGB & 255
        }
    }

    ;...
}

Subclassing Arrays of Arrays

When a multi-parameter assignment such as table[x, y] := content implicitly causes a new object to be created, the new object ordinarily has no base and therefore no custom methods or special behaviour. __Set may be used to initialize these objects, as demonstrated below.

x := {base: {addr: Func("x_Addr"), __Set: Func("x_Setter")}}

; Assign value, implicitly calling x_Setter to create sub-objects.
x[1,2,3] := "..."

; Retrieve value and call example method.
MsgBox % x[1,2,3] "`n" x.addr() "`n" x[1].addr() "`n" x[1,2].addr()

x_Setter(x, p1, p2, p3) {
    x[p1] := new x.base
}

x_Addr(x) {
    return &x
}

Since x_Setter has four mandatory parameters, it will only be called when there are two or more key parameters. When the assignment above occurs, the following takes place:

Default Base Object

When a non-object value is used with object syntax, the default base object is invoked. This can be used for debugging or to globally define object-like behaviour for strings, numbers and/or variables. The default base may be accessed by using .base with any non-object value; for instance, "".base. Although the default base cannot be set as in "".base := Object(), the default base may itself have a base as in "".base.base := Object().

Automatic Var Init

When an empty variable is used as the target of a set operation, it is passed directly to the __Set meta-function, giving it opportunity to insert a new object into the variable. For brevity, this example does not support multiple parameters; it could, by using a variadic function.

"".base.__Set := Func("Default_Set_AutomaticVarInit")

empty_var.foo := "bar"
MsgBox % empty_var.foo

Default_Set_AutomaticVarInit(ByRef var, key, value)
{
    if (var = "")
        var := Object(key, value)
}

Pseudo-Properties

Object "syntax sugar" can be applied to strings and numbers.

"".base.__Get := Func("Default_Get_PseudoProperty")
"".base.is    := Func("Default_is")

MsgBox % A_AhkPath.length " == " StrLen(A_AhkPath)
MsgBox % A_AhkPath.length.is("integer")

Default_Get_PseudoProperty(nonobj, key)
{
    if (key = "length")
        return StrLen(nonobj)
}

Default_is(nonobj, type)
{
    if nonobj is %type%
        return true
    return false
}

Note that built-in functions may also be used, but in this case the parentheses cannot be omitted:

"".base.length := Func("StrLen")
MsgBox % A_AhkPath.length() " == " StrLen(A_AhkPath)

Debugging

If allowing a value to be treated as an object is undesirable, a warning may be shown whenever a non-object value is invoked:

"".base.__Get := "".base.__Set := "".base.__Call := Func("Default__Warn")

empty_var.foo := "bar"
x := (1 + 1).is("integer")

Default__Warn(nonobj, p1="", p2="", p3="", p4="")
{
    ListLines
    MsgBox A non-object value was improperly invoked.`n`nSpecifically: %nonobj%
}

Implementation

Reference-Counting

AutoHotkey uses a basic reference-counting mechanism to automatically free the resources used by an object when it is no longer referenced by the script. Script authors should not invoke this mechanism explicitly, except when dealing directly with unmanaged pointers to objects.

Currently in AutoHotkey v1.1, temporary references created within an expression (but not stored anywhere) are released immediately after use. For example, Fn(&{}) passes an invalid address to the function, because the temporary reference returned by {} is released immediately after the address-of operator is evaluated.

To run code when the last reference to an object is being released, implement the __Delete meta-function.

Known Limitations:

Although memory used by the object is reclaimed by the operating system when the program exits, __Delete will not be called unless all references to the object are freed. This can be important if it frees other resources which are not automatically reclaimed by the operating system, such as temporary files.

Pointers to Objects

In some rare cases it may be necessary to pass an object to external code via DllCall() or store it in a binary data structure for later retrieval. An object's address can be retrieved via address := &object; however, this effectively makes two references to the object, but the program only knows about the one in object. If the last known reference to the object was released, the object would be deleted. Therefore, the script must inform the object that it has gained a reference. There are two ways to do this:

; Method #1: Explicitly increment the reference count.
address := &object
ObjAddRef(address)

; Method #2: Use Object(), which increments the reference count and returns an address.
address := Object(object)

This function can also be used to convert an address back into a reference:

object := Object(address)

Either way, the script must also inform the object when it is finished with that reference:

; Decrement the object's reference count to allow it to be freed:
ObjRelease(address)

Generally each new copy of an object's address should be treated as another reference to the object, so the script should call ObjAddRef() when it gains a copy and ObjRelease() immediately before losing one. For example, whenever an address is copied via something like x := address, ObjAddRef() should be called. Similarly, when the script is finished with x (or is about to overwrite x's value), it should call ObjRelease().

Note that the Object() function can be used even on objects which it did not create, such as COM objects and File objects.