diff --git a/scope.go b/scope.go index c7f9f60..ae38f92 100644 --- a/scope.go +++ b/scope.go @@ -80,3 +80,11 @@ func (s *Scope) FreeAll() { Free(pointer) } } + +// IsFreed reports whether FreeAll has been called. +func (s *Scope) IsFreed() bool { + if s == nil { + return true + } + return s.freed.Load() +} diff --git a/scope_test.go b/scope_test.go index d0b1c63..718fa76 100644 --- a/scope_test.go +++ b/scope_test.go @@ -37,3 +37,22 @@ func TestScopePanicsAfterFreeAll(t *testing.T) { scope.CString("nope") }) } + +func TestScopeIsFreedTracksLifecycle(t *testing.T) { + t.Parallel() + + scope := NewScope() + if scope.IsFreed() { + t.Fatal("expected new scope to be active") + } + + scope.FreeAll() + if !scope.IsFreed() { + t.Fatal("expected scope to report freed after FreeAll") + } + + var nilScope *Scope + if !nilScope.IsFreed() { + t.Fatal("expected nil scope to be treated as freed") + } +}