Initial commit

This commit is contained in:
2026-07-13 09:25:40 +07:00
parent c08ff54676
commit bccfb156d7
1938 changed files with 641646 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
using System;
using CeresSharp.Native;
namespace CeresSharp.Native.SafeHandles;
/// <summary>
/// Safe handle for Ceres LossFunction.
/// </summary>
internal sealed class LossFunctionHandle : BaseSafeHandle
{
private volatile bool _ownedByProblem = false;
private LossFunctionHandle() : base()
{
}
private LossFunctionHandle(IntPtr handle, bool ownsHandle) : base(handle, ownsHandle)
{
}
public static LossFunctionHandle Create(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
throw new Exceptions.CeresException(Exceptions.CeresErrorCode.NullPointer, "Loss function handle is null");
}
return new LossFunctionHandle(handle, true);
}
/// <summary>
/// Marks this loss function as owned by a Problem.
/// When owned by Problem, the native handle should NOT be freed here
/// because Problem/Ceres will free it when Problem is destroyed.
/// </summary>
internal void MarkOwnedByProblem() => _ownedByProblem = true;
protected override void ReleaseNativeHandle(IntPtr handle)
{
if (_ownedByProblem)
{
// Problem owns this loss function - Ceres will delete it when Problem is freed.
// Do NOT free here to avoid double-free.
return;
}
// Standalone loss function - we must free it ourselves
CeresNative.ceres_wrapper_free_loss_function(handle);
}
}