uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026

Warning

uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.

Owned

Method

Product: 

Fast Math Parser

Class: 

Item

Marks a uCalc object for automatic memory release when it goes out of scope, primarily for use in C++ environments.

Syntax

Owned()

Parameters

[None]

Return

Item

Returns the current Item instance to allow for method chaining.

Remarks

🛡️ Object Ownership and Automatic Release

The Owned() method is a crucial memory management tool, primarily for C++, that flags a uCalc object for automatic release when its corresponding variable goes out of scope. This aligns with the C++ RAII (Resource Acquisition Is Initialization) paradigm, preventing memory leaks by ensuring resources are cleaned up deterministically.

This method returns the current Item instance, enabling a fluent, chainable syntax.

⚙️ How It Works

By default, creating a uCalc object (like an Item, Expression, or Transformer) allocates resources that persist until you explicitly call .Release(). This is true even if the variable holding the object goes out of scope.

Calling Owned() on an object changes this behavior. The uCalc engine marks the object as "owned" by its current scope. When the scope is exited, the object's destructor is called, which in turn automatically calls Release().

Platform-Specific Usage: Owned() vs. using

The need for Owned() is specific to C++'s memory model. Other languages achieve the same result with different syntax:

  • C++: Use Owned() on a stack-allocated object or in conjunction with smart pointers.
    // C++ Example{   var myItem = new uCalc.Item("Var: x=5");   myItem.Owned();    // myItem will be released automatically at the end of this scope}
*   **C# / VB.NET**: Use the `using` statement (which relies on the `IDisposable` interface).    ```csharp    // C# Example    using (var myItem = new uCalc.Item("Var: x=5")) {    // myItem is automatically released here    }

⚖️ Comparative Analysis

Owned() vs. Standard C++ RAII

In native C++, you would typically wrap a raw pointer in a smart pointer like std::unique_ptr or std::shared_ptr to manage its lifetime. uCalc's Owned() method provides a similar, built-in mechanism that is simpler and more direct for uCalc objects. It avoids the need for boilerplate smart pointer declarations.

Owned() vs. C# IDisposable

The concept is identical. Owned() is uCalc's C++-idiomatic way of achieving what IDisposable and using do in .NET. It provides developers with a consistent cross-platform mental model for resource management, even if the syntax differs.

Best Practice: In C++, always call Owned() on stack-allocated uCalc objects or objects whose lifetime is tied to a specific scope to prevent resource leaks. For heap-allocated objects that need to persist beyond their initial scope, manual Release() is still required.

Examples