CreateManagedObject

CreateManagedObject[expr,f]

creates a managed object that evaluates f[expr] when it is no longer referenced.

Details

  • Many functions that expect pointer objects will automatically unwrap managed expressions containing pointers.
  • CreateManagedObject[expr,man] creates a new managed object representing expr, which shares a reference count with the ManagedObject man.
  • Only active managed objects evaluate the freeing function when they are no longer referenced.
  • Managed objects can be inactivated with UnmanageObject.

Examples

open allclose all

Basic Examples  (2)

Create a managed object:

Get the contained expression:

f[expr] is evaluated once the managed object is no longer referenced:

Possible Issues  (3)

Managed objects are often freed too soon when using With, where the reference count is decremented after the last use of the symbol in the body:

Module can be used instead, which only decrements the reference count at the end:

func[expr] is evaluated once the reference count of the managed expression object is zero. However, expr may exist outside of the managed expression, allowing it to continue even after its reference count is nominally zero:

There are many cases where the reference count would seem to be zero, but where it is not because of Out:

Access the managed object with Out:

Wolfram Research (2023), CreateManagedObject, Wolfram Language function, https://reference.wolfram.com/language/ref/CreateManagedObject.html.

Text

Wolfram Research (2023), CreateManagedObject, Wolfram Language function, https://reference.wolfram.com/language/ref/CreateManagedObject.html.

CMS

Wolfram Language. 2023. "CreateManagedObject." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/CreateManagedObject.html.

APA

Wolfram Language. (2023). CreateManagedObject. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CreateManagedObject.html

BibTeX

@misc{reference.wolfram_2024_createmanagedobject, author="Wolfram Research", title="{CreateManagedObject}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/CreateManagedObject.html}", note=[Accessed: 21-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_createmanagedobject, organization={Wolfram Research}, title={CreateManagedObject}, year={2023}, url={https://reference.wolfram.com/language/ref/CreateManagedObject.html}, note=[Accessed: 21-December-2024 ]}