Demonstrates the recommended practice of using a scoped block for automatic resource management, preventing memory leaks.
ID: 602
See: Release
using uCalcSoftware;
var uc = new uCalc();
// A 'NewUsing' block ensures the expression is automatically released at the end of the scope.
// This is the safest pattern to prevent memory leaks.
using (var expr = new uCalc.Expression("5 * 10")) {
Console.WriteLine($"Result within scope: {expr.Evaluate()}");
}
// The 'expr' object is now released and its handle is invalid.
Console.WriteLine("Expression has been automatically released.");
Result within scope: 50
Expression has been automatically released. using uCalcSoftware; var uc = new uCalc(); // A 'NewUsing' block ensures the expression is automatically released at the end of the scope. // This is the safest pattern to prevent memory leaks. using (var expr = new uCalc.Expression("5 * 10")) { Console.WriteLine($"Result within scope: {expr.Evaluate()}"); } // The 'expr' object is now released and its handle is invalid. Console.WriteLine("Expression has been automatically released.");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// A 'NewUsing' block ensures the expression is automatically released at the end of the scope.
// This is the safest pattern to prevent memory leaks.
{
uCalc::Expression expr("5 * 10");
expr.Owned(); // Causes expr to be released when it goes out of scope
cout << "Result within scope: " << expr.Evaluate() << endl;
}
// The 'expr' object is now released and its handle is invalid.
cout << "Expression has been automatically released." << endl;
}
Result within scope: 50
Expression has been automatically released. #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // A 'NewUsing' block ensures the expression is automatically released at the end of the scope. // This is the safest pattern to prevent memory leaks. { uCalc::Expression expr("5 * 10"); expr.Owned(); // Causes expr to be released when it goes out of scope cout << "Result within scope: " << expr.Evaluate() << endl; } // The 'expr' object is now released and its handle is invalid. cout << "Expression has been automatically released." << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// A 'NewUsing' block ensures the expression is automatically released at the end of the scope.
'// This is the safest pattern to prevent memory leaks.
Using expr As New uCalc.Expression("5 * 10")
Console.WriteLine($"Result within scope: {expr.Evaluate()}")
End Using
'// The 'expr' object is now released and its handle is invalid.
Console.WriteLine("Expression has been automatically released.")
End Sub
End Module
Result within scope: 50
Expression has been automatically released. Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// A 'NewUsing' block ensures the expression is automatically released at the end of the scope. '// This is the safest pattern to prevent memory leaks. Using expr As New uCalc.Expression("5 * 10") Console.WriteLine($"Result within scope: {expr.Evaluate()}") End Using '// The 'expr' object is now released and its handle is invalid. Console.WriteLine("Expression has been automatically released.") End Sub End Module