Rename CleanupEnvironment -> DestroyEnvironment

- Named the cleanup function something more consistent with the other
   cleanup functions.

 - Fixed a couple bugs where deferred stuff was executed after the
   environment was destroyed, causing a segfault.
This commit is contained in:
yalue
2023-02-04 12:41:35 -05:00
parent 137ec8f64f
commit e0cd5f977c
4 changed files with 20 additions and 20 deletions

View File

@@ -53,7 +53,7 @@ func main() {
onnxruntime.SetSharedLibraryPath("path/to/onnxruntime.so")
err := onnxruntime.InitializeEnvironment()
defer onnxruntime.CleanupEnvironment()
defer onnxruntime.DestroyEnvironment()
// We'll assume that network.onnx takes a single 2x3x4 input tensor and
// produces a 1x2x3 output tensor.

View File

@@ -52,6 +52,14 @@ func run() int {
return 1
}
fmt.Printf("The onnxruntime environment initialized OK.\n")
defer func() {
e := onnxruntime.DestroyEnvironment()
if e != nil {
fmt.Printf("Error destroying onnx environment: %s\n", e)
} else {
fmt.Printf("Environment cleaned up OK.\n")
}
}()
// Load the JSON with the test input and output data.
testInputs, e := loadInputsJSON(
@@ -98,15 +106,6 @@ func run() int {
fmt.Printf("Output value %d: expected %f, got %f\n", i,
outputTensor.GetData()[i], testInputs.FlattenedOutput[i])
}
// Ordinarily, it is probably fine to call this using defer, but we do it
// here just so we can print a status message after the cleanup completes.
e = onnxruntime.CleanupEnvironment()
if e != nil {
fmt.Printf("Error cleaning up the environment: %s\n", e)
return 1
}
fmt.Printf("The onnxruntime environment was cleaned up OK.\n")
return 0
}

View File

@@ -48,7 +48,7 @@ func SetSharedLibraryPath(path string) {
// Call this function to initialize the internal onnxruntime environment. If
// this doesn't return an error, the caller will be responsible for calling
// CleanupEnvironment to free the onnxruntime state when no longer needed.
// DestroyEnvironment to free the onnxruntime state when no longer needed.
func InitializeEnvironment() error {
if ortEnv != nil {
return fmt.Errorf("The onnxruntime has already been initialized")
@@ -69,7 +69,7 @@ func InitializeEnvironment() error {
status = C.CreateOrtMemoryInfo(&ortMemoryInfo)
if status != nil {
CleanupEnvironment()
DestroyEnvironment()
return fmt.Errorf("Error creating ORT memory info: %w",
statusToError(status))
}
@@ -79,7 +79,7 @@ func InitializeEnvironment() error {
// Call this function to cleanup the internal onnxruntime environment when it
// is no longer needed.
func CleanupEnvironment() error {
func DestroyEnvironment() error {
var e error
if ortMemoryInfo != nil {
C.ReleaseOrtMemoryInfo(ortMemoryInfo)

View File

@@ -82,7 +82,7 @@ func TestTensorTypes(t *testing.T) {
func TestCreateTensor(t *testing.T) {
InitializeRuntime(t)
defer CleanupEnvironment()
defer DestroyEnvironment()
s := NewShape(1, 2, 3)
tensor1, e := NewEmptyTensor[uint8](s)
if e != nil {
@@ -135,6 +135,13 @@ func TestCreateTensor(t *testing.T) {
func TestExampleNetwork(t *testing.T) {
InitializeRuntime(t)
defer func() {
e := DestroyEnvironment()
if e != nil {
t.Logf("Error cleaning up environment: %s\n", e)
t.FailNow()
}
}()
// Create input and output tensors
inputs := parseInputsJSON("test_data/example_network_results.json", t)
@@ -169,10 +176,4 @@ func TestExampleNetwork(t *testing.T) {
t.Logf("The neural network didn't produce the correct result: %s\n", e)
t.FailNow()
}
e = CleanupEnvironment()
if e != nil {
t.Logf("Failed cleaning up the environment: %s\n", e)
t.FailNow()
}
}