Skip to content

Commit

Permalink
fix: decoder potential memory leaks when decoder is being reused (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
muktihari authored Aug 15, 2024
1 parent 01bf787 commit b99ddd8
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,22 @@ func (d *Decoder) reset() {
d.sequenceCompleted = false
d.err = nil
d.fileHeader = proto.FileHeader{}
d.messages = nil
d.crc = 0
d.fileId = nil
d.localMessageDefinitions = [proto.LocalMesgNumMask + 1]*proto.MessageDefinition{}
d.developerDataIndexes = d.developerDataIndexes[:0]
d.fieldDescriptions = d.fieldDescriptions[:0]
}

// releaseTemporaryObjects releases objects that being created on a single decode process
// by stops referencing those objects so it can be released on next GC cycle.
func (d *Decoder) releaseTemporaryObjects() {
d.localMessageDefinitions = [proto.LocalMesgNumMask + 1]*proto.MessageDefinition{}
d.fieldsArray = [255]proto.Field{}
d.developerFieldsArray = [255]proto.DeveloperField{}
d.messages = nil
for i := range d.fieldDescriptions {
d.fieldDescriptions[i] = nil // avoid memory leaks
d.fieldDescriptions[i] = nil
}
d.fieldDescriptions = d.fieldDescriptions[:0]
}

// CheckIntegrity checks all FIT sequences of given reader are valid determined by these following checks:
Expand Down Expand Up @@ -383,6 +389,7 @@ func (d *Decoder) Decode() (*proto.FIT, error) {
if d.err = d.decodeFileHeaderOnce(); d.err != nil {
return nil, d.err
}
defer d.releaseTemporaryObjects()
if d.err = d.decodeMessages(); d.err != nil {
return nil, d.err
}
Expand Down Expand Up @@ -990,6 +997,7 @@ func (d *Decoder) DecodeWithContext(ctx context.Context) (*proto.FIT, error) {
if d.err = d.decodeFileHeaderOnce(); d.err != nil {
return nil, d.err
}
defer d.releaseTemporaryObjects()
if d.err = d.decodeMessagesWithContext(ctx); d.err != nil {
return nil, d.err
}
Expand Down

0 comments on commit b99ddd8

Please sign in to comment.