Class

# WeakRef

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Allows you to retain a reference to an object without requiring it to stay alive.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                   | Type                                              | Read-Only | Shared |
|------------------------|---------------------------------------------------|-----------|--------|
| `Value<weakref.value>` | `Object</api/data_types/additional_types/object>` | ✓         |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                | Parameters                                               | Returns | Shared |
|-------------------------------------|----------------------------------------------------------|---------|--------|
| `Constructor<weakref.constructor0>` | obj As `Object</api/data_types/additional_types/object>` |         |        |

## Property descriptions

<div id="weakref.value">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WeakRef.Value

**Value** As `Object</api/data_types/additional_types/object>`

> Returns the target object or `Nil</api/language/nil>` if the target no longer exists.
>
> This property is read-only.

## Method descriptions

<div id="weakref.constructor0">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WeakRef.Constructor

**Constructor**(obj as `Object</api/data_types/additional_types/object>`)

> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> `Constructors</api/language/constructor>` are special methods called when you create an object with the `New</api/language/new>` keyword and pass in the parameters above.
>
> </div>
>
> Creates a <span class="title-ref">WeakRef</span> instance.
>
> The target object that you want to retain a reference to after it has been destroyed.
>
> This example creates a weak reference to a `SQLiteDatabase</api/databases/sqlitedatabase>` object. The property "db As SQLiteDatabase" is a property on the `App</api/language/app>` object.
>
> ``` xojo
> Var dbRef As WeakRef
>
> ' Create Database Object
> DB = New SQLiteDatabase
> dbRef = New WeakRef(DB)
>
> ' Set Database File
> DB.DatabaseFile = New FolderItem("MyDatabase.sqlite", FolderItem.PathModes.Native)
> ```

## Notes

<span class="title-ref">WeakRef</span> is useful when you want to find out when an object (such as a database or TCP/IP connection) is still in use somewhere in the application. The reference returned by <span class="title-ref">WeakRef</span> goes to `Nil</api/language/nil>` as soon as there are no longer any references to the object. This is the signal that you can do any necessary cleanup work.

When the object is destroyed, the <span class="title-ref">WeakRef</span>'s value will change to `Nil</api/language/nil>` after all ordinary references have been released and the object has been destroyed. Otherwise, the Value property returns the target object.

## Sample code

This code declares an instance of a FolderItem that will go out of scope and have its reference counter decreased. A weak reference is assigned to the FolderItem instance. While the instance is available, its name is displayed. When the instance is removed from memory, the weak reference value become `Nil</api/language/nil>`, so you now know there are no more references to it:

``` xojo
Var ref As WeakRef
If True Then
  Var f As New FolderItem
  f.Name = "TestFile.txt"
  ref = New WeakRef(f)
  If ref.Value <> Nil Then
    MessageBox(FolderItem(ref.Value).Name) ' This is shown
  Else
    MessageBox("f is Nil.")
  End If
End If

If ref.Value <> Nil Then
  MessageBox(FolderItem(ref.Value).Name)
Else
  MessageBox("f is Nil.") ' This is shown
End If
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `Creating Weak References to objects</topics/advanced_features/creating_weak_references_to_objects>`, `How Xojo Manages Memory</topics/debugging/how_xojo_manages_memory>` topics

</div>
