Skip to content

Commit

Permalink
defer freeAlloc
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaasman00 committed Sep 24, 2024
1 parent 197fd54 commit 4eb4c19
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
6 changes: 2 additions & 4 deletions cmd/opampsupervisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ func runInteractive() error {

supervisor, err := supervisor.NewSupervisor(logger, *configFlag)
if err != nil {
logger.Error(err.Error())
os.Exit(-1)
return err
}

err = supervisor.Start()
if err != nil {
logger.Error(err.Error())
os.Exit(-1)
return err
}

interrupt := make(chan os.Signal, 1)
Expand Down
9 changes: 5 additions & 4 deletions cmd/opampsupervisor/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func run() error {
if err := allocConsole(); err != nil {
return fmt.Errorf("alloc console: %w", err)
}
defer func() {
_ = freeConsole()
}()

// No need to supply service name when startup is invoked through
// the Service Control Manager directly.
Expand All @@ -35,16 +38,14 @@ func run() error {
// Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value
// this means that the process is not running as a service, so run interactively.

// deallocate console if we're not running as service
// manually free console before running interactively
if err = freeConsole(); err != nil {
return fmt.Errorf("free console: %w", err)
}
return runInteractive()
}

return fmt.Errorf("failed to start supervisor: %w", err)
}

return nil
}

Expand All @@ -58,7 +59,7 @@ func allocConsole() error {
return nil
}

// deallocate console once we're done with it
// free console once we're done with it
func freeConsole() error {
ret, _, err := freeConsoleProc.Call()
if ret == 0 {
Expand Down

0 comments on commit 4eb4c19

Please sign in to comment.