Bug 167744 - Correct memory leak in MediaConstraints
Summary: Correct memory leak in MediaConstraints
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Media (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: Brent Fulgham
URL:
Keywords: InRadar
Depends on: 167753
Blocks:
  Show dependency treegraph
 
Reported: 2017-02-02 10:50 PST by Brent Fulgham
Modified: 2017-02-03 12:55 PST (History)
6 users (show)

See Also:


Attachments
Patch (1.66 KB, patch)
2017-02-02 10:55 PST, Brent Fulgham
no flags Details | Formatted Diff | Diff
Patch (4.16 KB, patch)
2017-02-02 17:37 PST, Brent Fulgham
andersca: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Brent Fulgham 2017-02-02 10:50:46 PST
Static analysis discovered a memory leak in the handling of ConstraintHolders.

1. ConstraintHolder::create returns a reference to a “new” object.
2. m_variants holds a set of ConstraintHolder objects.
3. m_variants.append(ConstraintHolder::create(…)) copies the returned reference, leaking the new object.

I discussed this with Anders Carlsson, who confirmed that we should just return the object by value here, and return-value-optimization will make sure we efficiently use the memory.
Comment 1 Brent Fulgham 2017-02-02 10:52:49 PST
<rdar://problem/30331444>
Comment 2 Brent Fulgham 2017-02-02 10:55:19 PST
Created attachment 300419 [details]
Patch
Comment 3 Anders Carlsson 2017-02-02 10:59:25 PST
Comment on attachment 300419 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=300419&action=review

> Source/WebCore/platform/mediastream/MediaConstraints.h:716
> +        static ConstraintHolder create(const MediaConstraint& value) { return *new ConstraintHolder(value); }

This should just be return ConstraintHolder(value) otherwise we'll still leak.
Comment 4 Brent Fulgham 2017-02-02 11:34:35 PST
Committed r211579: <http://trac.webkit.org/changeset/211579>
Comment 5 Brent Fulgham 2017-02-02 11:35:15 PST
(In reply to comment #3)
> Comment on attachment 300419 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=300419&action=review
> 
> > Source/WebCore/platform/mediastream/MediaConstraints.h:716
> > +        static ConstraintHolder create(const MediaConstraint& value) { return *new ConstraintHolder(value); }
> 
> This should just be return ConstraintHolder(value) otherwise we'll still
> leak.

Done!
Comment 6 Ryan Haddad 2017-02-02 13:15:57 PST
(In reply to comment #4)
> Committed r211579: <http://trac.webkit.org/changeset/211579>

This change caused 5 mediastream LayoutTests to crash:

https://build.webkit.org/results/Apple%20Sierra%20Release%20WK2%20(Tests)/r211582%20(3234)/results.html
Comment 7 WebKit Commit Bot 2017-02-02 13:30:33 PST
Re-opened since this is blocked by bug 167753
Comment 8 Brent Fulgham 2017-02-02 13:31:42 PST
This isn't quite right. I introduced some media stream crashes with this change, so I'm rolling it out under https://bugs.webkit.org/show_bug.cgi?id=167753.
Comment 9 Brent Fulgham 2017-02-02 17:37:05 PST
Created attachment 300477 [details]
Patch
Comment 10 Brent Fulgham 2017-02-02 17:39:05 PST
The problem was that the move operator was not clearing the union, so when the "move source" was cleaned up, the memory was getting freed, even though the pointers had been moved to the "move target" location.
Comment 11 Brent Fulgham 2017-02-02 20:22:40 PST
Tests are happy. I just want to have Anders (or maybe Darin) check that the move operator is written properly.
Comment 12 Anders Carlsson 2017-02-03 12:39:12 PST
Comment on attachment 300477 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=300477&action=review

> Source/WebCore/platform/mediastream/MediaConstraints.h:749
> +                m_value.asInteger = WTFMove(other.m_value.asInteger);
> +                other.m_value.asInteger = nullptr;

No need to use move with pointers, just do something like

m_value.asInteger = std::exchange(other.m_value.asInteger, nullptr);

(same thing for the other cases).
Comment 13 Brent Fulgham 2017-02-03 12:41:21 PST
Comment on attachment 300477 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=300477&action=review

>> Source/WebCore/platform/mediastream/MediaConstraints.h:749
>> +                other.m_value.asInteger = nullptr;
> 
> No need to use move with pointers, just do something like
> 
> m_value.asInteger = std::exchange(other.m_value.asInteger, nullptr);
> 
> (same thing for the other cases).

Oh, cool! Will change.
Comment 14 Brent Fulgham 2017-02-03 12:55:02 PST
Committed r211646: <http://trac.webkit.org/changeset/211646>