Skip to content

Commit

Permalink
Merge pull request #154 from kerberos-io/fix/memory-leak-onvif
Browse files Browse the repository at this point in the history
Memory leak on SendSoap ONVIF library
  • Loading branch information
cedricve authored Jan 16, 2025
2 parents 9cb3c97 + 666ff20 commit 28b2137
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion machinery/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/gorilla/websocket v1.5.3
github.com/kellydunn/golang-geo v0.7.0
github.com/kerberos-io/joy4 v1.0.64
github.com/kerberos-io/onvif v0.0.16
github.com/kerberos-io/onvif v0.0.17
github.com/minio/minio-go/v6 v6.0.57
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/pion/rtp v1.8.10
Expand Down
4 changes: 2 additions & 2 deletions machinery/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ github.com/kellydunn/golang-geo v0.7.0 h1:A5j0/BvNgGwY6Yb6inXQxzYwlPHc6WVZR+Mrar
github.com/kellydunn/golang-geo v0.7.0/go.mod h1:YYlQPJ+DPEzrHx8kT3oPHC/NjyvCCXE+IuKGKdrjrcU=
github.com/kerberos-io/joy4 v1.0.64 h1:gTUSotHSOhp9mNqEecgq88tQHvpj7TjmrvPUsPm0idg=
github.com/kerberos-io/joy4 v1.0.64/go.mod h1:nZp4AjvKvTOXRrmDyAIOw+Da+JA5OcSo/JundGfOlFU=
github.com/kerberos-io/onvif v0.0.16 h1:fSiyc5ja6Q6ux6Jscvv4TVDPeK8O9x8WT1fG7YsbC8Q=
github.com/kerberos-io/onvif v0.0.16/go.mod h1:P1kUcCfeotJSlL1jwGseH6NSnCwWiuJLl3gAzafnLbA=
github.com/kerberos-io/onvif v0.0.17 h1:BK91WK+cO4lPvhVTh0dPtALRxjGIlFdXiA3O9tvq2UQ=
github.com/kerberos-io/onvif v0.0.17/go.mod h1:P1kUcCfeotJSlL1jwGseH6NSnCwWiuJLl3gAzafnLbA=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g=
Expand Down
66 changes: 34 additions & 32 deletions machinery/src/onvif/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ func ConnectToOnvifDevice(cameraConfiguration *models.IPCamera) (*onvif.Device,
var b []byte
if resp != nil {
b, err = io.ReadAll(resp.Body)
resp.Body.Close() // Ensure the response body is closed
if err != nil {
log.Log.Error("onvif.ConnectToOnvifDevice(): " + err.Error())
}
resp.Body.Close()
}
stringBody := string(b)
decodedXML, et, err := getXMLNode(stringBody, "GetCapabilitiesResponse")
Expand All @@ -252,10 +252,10 @@ func GetTokenFromProfile(device *onvif.Device, profileId int) (xsdonvif.Referenc
// Get Profiles
resp, err := device.CallMethod(media.GetProfiles{})
if err == nil {
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err == nil {
stringBody := string(b)
resp.Body.Close() // Ensure the response body is closed
decodedXML, et, err := getXMLNode(stringBody, "GetProfilesResponse")
if err != nil {
log.Log.Debug("onvif.GetTokenFromProfile(): " + err.Error())
Expand Down Expand Up @@ -288,21 +288,19 @@ func GetPTZConfigurationsFromDevice(device *onvif.Device) (ptz.GetConfigurations
var b []byte
if resp != nil {
b, err = io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body.Close() // Ensure the response body is closed
}

if err == nil {
if err == nil {
stringBody := string(b)
decodedXML, et, err := getXMLNode(stringBody, "GetConfigurationsResponse")
if err != nil {
stringBody := string(b)
decodedXML, et, err := getXMLNode(stringBody, "GetConfigurationsResponse")
if err != nil {
log.Log.Debug("onvif.GetPTZConfigurationsFromDevice(): " + err.Error())
return configurations, err
} else {
if err := decodedXML.DecodeElement(&configurations, et); err != nil {
log.Log.Debug("onvif.GetPTZConfigurationsFromDevice(): " + err.Error())
return configurations, err
} else {
if err := decodedXML.DecodeElement(&configurations, et); err != nil {
log.Log.Debug("onvif.GetPTZConfigurationsFromDevice(): " + err.Error())
return configurations, err
}
}
}
}
Expand Down Expand Up @@ -360,7 +358,7 @@ func GetPosition(device *onvif.Device, token xsdonvif.ReferenceToken) (xsdonvif.
var b []byte
if resp != nil {
b, err = io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body.Close() // Ensure the response body is closed
}

if err == nil {
Expand Down Expand Up @@ -794,7 +792,7 @@ func GetPresetsFromDevice(device *onvif.Device) ([]models.OnvifActionPreset, err
var b []byte
if resp != nil {
b, err = io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body.Close() // Ensure the response body is closed
}
if err == nil {
stringBody := string(b)
Expand Down Expand Up @@ -845,7 +843,7 @@ func GoToPresetFromDevice(device *onvif.Device, presetName string) error {
var b []byte
if resp != nil {
b, err = io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body.Close() // Ensure the response body is closed
}
if err == nil {
stringBody := string(b)
Expand Down Expand Up @@ -1004,7 +1002,7 @@ func CreatePullPointSubscription(dev *onvif.Device) (string, error) {
var b2 []byte
if resp != nil {
b2, err = io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body.Close() // Ensure the response body is closed
if err == nil {
stringBody := string(b2)
decodedXML, et, err := getXMLNode(stringBody, "CreatePullPointSubscriptionResponse")
Expand All @@ -1023,19 +1021,25 @@ func CreatePullPointSubscription(dev *onvif.Device) (string, error) {
}

func UnsubscribePullPoint(dev *onvif.Device, pullPointAddress string) error {

// Unsubscribe from the device
unsubscribe := event.Unsubscribe{}
requestBody, err := xml.Marshal(unsubscribe)
if err != nil {
log.Log.Error("onvif.main.UnsubscribePullPoint(): " + err.Error())
}

res, err := dev.SendSoap(pullPointAddress, string(requestBody))
if err != nil {
log.Log.Error("onvif.main.UnsubscribePullPoint(): " + err.Error())
}
if res != nil {
_, err := io.ReadAll(res.Body)
res.Body.Close()
b, err := io.ReadAll(res.Body)
res.Body.Close() // Ensure the response body is closed
if err == nil {
stringBody := string(b)
log.Log.Debug("onvif.main.UnsubscribePullPoint(): " + stringBody)
}
if err != nil {
log.Log.Error("onvif.main.UnsubscribePullPoint(): " + err.Error())
}
Expand Down Expand Up @@ -1099,7 +1103,7 @@ func GetEventMessages(dev *onvif.Device, pullPointAddress string) ([]ONVIFEvents
var pullMessagesResponse event.PullMessagesResponse
if res != nil {
bs, err := io.ReadAll(res.Body)
res.Body.Close()
res.Body.Close() // Ensure the response body is closed
if err == nil {
stringBody := string(bs)
decodedXML, et, err := getXMLNode(stringBody, "PullMessagesResponse")
Expand Down Expand Up @@ -1208,7 +1212,7 @@ func GetDigitalInputs(dev *onvif.Device) (device.GetDigitalInputsResponse, error
resp, err := dev.CallMethod(deviceio.GetDigitalInputs{})
if resp != nil {
b, err = io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body.Close() // Ensure the response body is closed
}

if err == nil {
Expand Down Expand Up @@ -1240,21 +1244,19 @@ func GetRelayOutputs(dev *onvif.Device) (device.GetRelayOutputsResponse, error)
var b []byte
if resp != nil {
b, err = io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body.Close() // Ensure the response body is closed
}

if err == nil {
if err == nil {
stringBody := string(b)
decodedXML, et, err := getXMLNode(stringBody, "GetRelayOutputsResponse")
if err != nil {
log.Log.Error("onvif.main.GetRelayOutputs(): " + err.Error())
stringBody := string(b)
decodedXML, et, err := getXMLNode(stringBody, "GetRelayOutputsResponse")
if err != nil {
log.Log.Error("onvif.main.GetRelayOutputs(): " + err.Error())
return relayoutputs, err
} else {
if err := decodedXML.DecodeElement(&relayoutputs, et); err != nil {
log.Log.Debug("onvif.main.GetRelayOutputs(): " + err.Error())
return relayoutputs, err
} else {
if err := decodedXML.DecodeElement(&relayoutputs, et); err != nil {
log.Log.Debug("onvif.main.GetRelayOutputs(): " + err.Error())
return relayoutputs, err
}
}
}
}
Expand Down Expand Up @@ -1283,7 +1285,7 @@ func TriggerRelayOutput(dev *onvif.Device, output string) (err error) {
var b []byte
if errResp != nil {
b, err = io.ReadAll(resp.Body)
resp.Body.Close()
resp.Body.Close() // Ensure the response body is closed
}
stringBody := string(b)
if err == nil && resp.StatusCode == 200 {
Expand Down

0 comments on commit 28b2137

Please sign in to comment.