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:
Class:
Manages automatic resource release for an object, enabling RAII-style lifetime management primarily for C++.
Syntax
Parameters
Return
uCalcBase
Returns the current instance to allow for method chaining.
Remarks
🛡️ Managing Object Lifetime: The Owned Method
In uCalc, every object (like a uCalc instance, Expression, or Transformer) consumes resources. Proper resource management is crucial to prevent memory leaks. By default, you must manually call Release() on an object when it's no longer needed.
The Owned() method provides a way to opt into automatic resource management, aligning with the native idioms of different programming languages.
C++: Embracing RAII
Owned() is designed specifically for C++ developers to leverage the RAII (Resource Acquisition Is Initialization) pattern. When you declare an object as "owned", its destructor will automatically call Release(), ensuring resources are freed when the object goes out of scope.
// An object created on the stack is a candidate for RAII.uCalc myCalc;myCalc.Owned(); // Now, myCalc will be released automatically at the end of the scope.This is essential when dealing with objects returned by methods like Clone(), which are created on the heap. By wrapping the returned handle in a stack-allocated object and calling Owned(), you transfer ownership to the stack, guaranteeing cleanup.
C# and VB.NET: The using Keyword
In .NET languages, Owned() is not needed. The idiomatic way to ensure automatic resource cleanup is to use the using statement (C#) or Using block (VB.NET). All major uCalc objects implement IDisposable, which integrates directly with this language feature.
// The 'using' statement ensures myCalc.Release() is called automatically.using (var myCalc = new uCalc()){ // ... use myCalc here ...} // myCalc is released here.Comparative Analysis: Why Owned()?
vs. Standard Smart Pointers (std::unique_ptr)
A C++ developer might ask, "Why not just return a std::unique_ptr<uCalc>?" uCalc uses an internal, cross-platform reference counting and object management system. The Owned() method acts as a bridge, allowing the C++-specific RAII pattern to interface cleanly with uCalc's internal lifetime management without exposing implementation details or forcing a dependency on a specific smart pointer type.
vs. Manual Release() Calls
Forgetting to call Release() is a common source of memory leaks. Using Owned() in C++ (or using in C#) makes resource management declarative and less error-prone. The cleanup is guaranteed by the language's scoping rules, leading to safer and more robust code.
Examples
"using" (C#) and Owned (C++) for auto-releasing uCalc object
using uCalcSoftware;
var uc = new uCalc();
// In C# and VB you should use "using".
// In C++ you can flag a uCalc object for
// auto-release with Owned(), or by setting
// the last parameter of the constructor to true.
uc.IsDefault = true; // Set uc as the default uCalc object
uCalc.DefaultInstance.DefineVariable("Value = 'Original uc object'");
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")); // Outputs: Original uc object
// Use "using" so that the object is auto-released when it it goes out of scope
using (var uCalcTemp = new uCalc()) {
uCalcTemp.IsDefault = true; // Set uCalcTemp as the default uCalc object
uCalc.DefaultInstance.DefineVariable("Value = 'uCalcTemp object'");
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")); // uCalcTemp object
}
// uCalcTemp goes out of scope here, and the default uCalc object reverts back to uc
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")); // Original uc object
{
/*using*/ var uCalcSticky = new uCalc(); // remains the default even after going out of scope
uCalcSticky.IsDefault = true; // Set uCalcSticky as the default uCalc object
uCalc.DefaultInstance.DefineVariable("Value = 'uCalcSticky object'");
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")); // Outputs: uCalcSticky object
} // The uCalcSticky object itself goes out of scope here, but internally it remains the default uCalc object
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value"));
Original uc object
uCalcTemp object
Original uc object
uCalcSticky object
uCalcSticky object using uCalcSoftware; var uc = new uCalc(); // In C# and VB you should use "using". // In C++ you can flag a uCalc object for // auto-release with Owned(), or by setting // the last parameter of the constructor to true. uc.IsDefault = true; // Set uc as the default uCalc object uCalc.DefaultInstance.DefineVariable("Value = 'Original uc object'"); Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")); // Outputs: Original uc object // Use "using" so that the object is auto-released when it it goes out of scope using (var uCalcTemp = new uCalc()) { uCalcTemp.IsDefault = true; // Set uCalcTemp as the default uCalc object uCalc.DefaultInstance.DefineVariable("Value = 'uCalcTemp object'"); Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")); // uCalcTemp object } // uCalcTemp goes out of scope here, and the default uCalc object reverts back to uc Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")); // Original uc object { /*using*/ var uCalcSticky = new uCalc(); // remains the default even after going out of scope uCalcSticky.IsDefault = true; // Set uCalcSticky as the default uCalc object uCalc.DefaultInstance.DefineVariable("Value = 'uCalcSticky object'"); Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")); // Outputs: uCalcSticky object } // The uCalcSticky object itself goes out of scope here, but internally it remains the default uCalc object Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// In C# and VB you should use "using".
// In C++ you can flag a uCalc object for
// auto-release with Owned(), or by setting
// the last parameter of the constructor to true.
uc.IsDefault(true); // Set uc as the default uCalc object
uCalc::DefaultInstance().DefineVariable("Value = 'Original uc object'");
cout << uCalc::DefaultInstance().EvalStr("Value") << endl; // Outputs: Original uc object
{
uCalc uCalcTemp;
uCalcTemp.Owned(); // Causes uCalcTemp to be released when it goes out of scope // Make uCalcTemp owned so it reverts back to uc when uCalcTemp goes out of scope
uCalcTemp.IsDefault(true); // Set uCalcTemp as the default uCalc object
uCalc::DefaultInstance().DefineVariable("Value = 'uCalcTemp object'");
cout << uCalc::DefaultInstance().EvalStr("Value") << endl; // uCalcTemp object
}
// uCalcTemp goes out of scope here, and the default uCalc object reverts back to uc
cout << uCalc::DefaultInstance().EvalStr("Value") << endl; // Original uc object
{
uCalc uCalcSticky; // remains the default even after going out of scope
uCalcSticky.IsDefault(true); // Set uCalcSticky as the default uCalc object
uCalc::DefaultInstance().DefineVariable("Value = 'uCalcSticky object'");
cout << uCalc::DefaultInstance().EvalStr("Value") << endl; // Outputs: uCalcSticky object
} // The uCalcSticky object itself goes out of scope here, but internally it remains the default uCalc object
cout << uCalc::DefaultInstance().EvalStr("Value") << endl;
}
Original uc object
uCalcTemp object
Original uc object
uCalcSticky object
uCalcSticky object #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // In C# and VB you should use "using". // In C++ you can flag a uCalc object for // auto-release with Owned(), or by setting // the last parameter of the constructor to true. uc.IsDefault(true); // Set uc as the default uCalc object uCalc::DefaultInstance().DefineVariable("Value = 'Original uc object'"); cout << uCalc::DefaultInstance().EvalStr("Value") << endl; // Outputs: Original uc object { uCalc uCalcTemp; uCalcTemp.Owned(); // Causes uCalcTemp to be released when it goes out of scope // Make uCalcTemp owned so it reverts back to uc when uCalcTemp goes out of scope uCalcTemp.IsDefault(true); // Set uCalcTemp as the default uCalc object uCalc::DefaultInstance().DefineVariable("Value = 'uCalcTemp object'"); cout << uCalc::DefaultInstance().EvalStr("Value") << endl; // uCalcTemp object } // uCalcTemp goes out of scope here, and the default uCalc object reverts back to uc cout << uCalc::DefaultInstance().EvalStr("Value") << endl; // Original uc object { uCalc uCalcSticky; // remains the default even after going out of scope uCalcSticky.IsDefault(true); // Set uCalcSticky as the default uCalc object uCalc::DefaultInstance().DefineVariable("Value = 'uCalcSticky object'"); cout << uCalc::DefaultInstance().EvalStr("Value") << endl; // Outputs: uCalcSticky object } // The uCalcSticky object itself goes out of scope here, but internally it remains the default uCalc object cout << uCalc::DefaultInstance().EvalStr("Value") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// In C# and VB you should use "using".
'// In C++ you can flag a uCalc object for
'// auto-release with Owned(), or by setting
'// the last parameter of the constructor to true.
uc.IsDefault = true '// Set uc as the default uCalc object
uCalc.DefaultInstance.DefineVariable("Value = 'Original uc object'")
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) '// Outputs: Original uc object
'// Use "using" so that the object is auto-released when it it goes out of scope
Using uCalcTemp As New uCalc()
uCalcTemp.IsDefault = true '// Set uCalcTemp as the default uCalc object
uCalc.DefaultInstance.DefineVariable("Value = 'uCalcTemp object'")
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) '// uCalcTemp object
End Using
'// uCalcTemp goes out of scope here, and the default uCalc object reverts back to uc
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) '// Original uc object
If True Then
Dim uCalcSticky As New uCalc() '// remains the default even after going out of scope
uCalcSticky.IsDefault = true '// Set uCalcSticky as the default uCalc object
uCalc.DefaultInstance.DefineVariable("Value = 'uCalcSticky object'")
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) '// Outputs: uCalcSticky object
End If '// The uCalcSticky object itself goes out of scope here, but internally it remains the default uCalc object
Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value"))
End Sub
End Module
Original uc object
uCalcTemp object
Original uc object
uCalcSticky object
uCalcSticky object Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// In C# and VB you should use "using". '// In C++ you can flag a uCalc object for '// auto-release with Owned(), or by setting '// the last parameter of the constructor to true. uc.IsDefault = true '// Set uc as the default uCalc object uCalc.DefaultInstance.DefineVariable("Value = 'Original uc object'") Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) '// Outputs: Original uc object '// Use "using" so that the object is auto-released when it it goes out of scope Using uCalcTemp As New uCalc() uCalcTemp.IsDefault = true '// Set uCalcTemp as the default uCalc object uCalc.DefaultInstance.DefineVariable("Value = 'uCalcTemp object'") Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) '// uCalcTemp object End Using '// uCalcTemp goes out of scope here, and the default uCalc object reverts back to uc Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) '// Original uc object If True Then Dim uCalcSticky As New uCalc() '// remains the default even after going out of scope uCalcSticky.IsDefault = true '// Set uCalcSticky as the default uCalc object uCalc.DefaultInstance.DefineVariable("Value = 'uCalcSticky object'") Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) '// Outputs: uCalcSticky object End If '// The uCalcSticky object itself goes out of scope here, but internally it remains the default uCalc object Console.WriteLine(uCalc.DefaultInstance.EvalStr("Value")) End Sub End Module