Bug 1317660 - Fix CID 1394336: Resource leaks in TestAudioPacketizer.cpp draft
authorJames Cheng <jacheng@mozilla.com>
Tue, 15 Nov 2016 18:20:32 +0800
changeset 439516 0af4aad54c57266d4f314a0d7b7ad3af388c3ea8
parent 438818 5e76768327660437bf3486554ad318e4b70276e1
child 537182 a4ea6e9d85d05a0d056bd525059686503c6eeee9
push id36019
push userbmo:jacheng@mozilla.com
push dateWed, 16 Nov 2016 04:12:57 +0000
bugs1317660, 1394336
milestone53.0a1
Bug 1317660 - Fix CID 1394336: Resource leaks in TestAudioPacketizer.cpp MozReview-Commit-ID: GAKgI6JVm52
dom/media/gtest/TestAudioPacketizer.cpp
--- a/dom/media/gtest/TestAudioPacketizer.cpp
+++ b/dom/media/gtest/TestAudioPacketizer.cpp
@@ -1,15 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include <stdint.h>
 #include <math.h>
+#include <memory>
 #include "../AudioPacketizer.h"
 #include "gtest/gtest.h"
 
 using namespace mozilla;
 
 template<typename T>
 class AutoBuffer
 {
@@ -32,26 +33,26 @@ int16_t Sequence(int16_t* aBuffer, uint3
 {
   uint32_t i;
   for (i = 0; i < aSize; i++) {
     aBuffer[i] = aStart + i;
   }
   return aStart + i;
 }
 
-void IsSequence(int16_t* aBuffer, uint32_t aSize, uint32_t aStart = 0)
+void IsSequence(std::unique_ptr<int16_t[]> aBuffer, uint32_t aSize, uint32_t aStart = 0)
 {
   for (uint32_t i = 0; i < aSize; i++) {
     ASSERT_TRUE(aBuffer[i] == static_cast<int64_t>(aStart + i)) <<
       "Buffer is not a sequence at offset " << i << std::endl;
   }
   // Buffer is a sequence.
 }
 
-void Zero(int16_t* aBuffer, uint32_t aSize)
+void Zero(std::unique_ptr<int16_t[]> aBuffer, uint32_t aSize)
 {
   for (uint32_t i = 0; i < aSize; i++) {
     ASSERT_TRUE(aBuffer[i] == 0) <<
       "Buffer is not null at offset " << i << std::endl;
   }
 }
 
 double sine(uint32_t aPhase) {
@@ -60,79 +61,73 @@ double sine(uint32_t aPhase) {
 
 TEST(AudioPacketizer, Test)
 {
   for (int16_t channels = 1; channels < 2; channels++) {
     // Test that the packetizer returns zero on underrun
     {
       AudioPacketizer<int16_t, int16_t> ap(441, channels);
       for (int16_t i = 0; i < 10; i++) {
-        int16_t* out = ap.Output();
-        Zero(out, 441);
-        delete[] out;
+        std::unique_ptr<int16_t[]> out(ap.Output());
+        Zero(std::move(out), 441);
       }
     }
     // Simple test, with input/output buffer size aligned on the packet size,
     // alternating Input and Output calls.
     {
       AudioPacketizer<int16_t, int16_t> ap(441, channels);
       int16_t seqEnd = 0;
       for (int16_t i = 0; i < 10; i++) {
         AutoBuffer<int16_t> b(441 * channels);
         int16_t prevEnd = seqEnd;
         seqEnd = Sequence(b.Get(), channels * 441, prevEnd);
         ap.Input(b.Get(), 441);
-        int16_t* out = ap.Output();
-        IsSequence(out, 441 * channels, prevEnd);
-        delete[] out;
+        std::unique_ptr<int16_t[]> out(ap.Output());
+        IsSequence(std::move(out), 441 * channels, prevEnd);
       }
     }
     // Simple test, with input/output buffer size aligned on the packet size,
     // alternating two Input and Output calls.
     {
       AudioPacketizer<int16_t, int16_t> ap(441, channels);
       int16_t seqEnd = 0;
       for (int16_t i = 0; i < 10; i++) {
         AutoBuffer<int16_t> b(441 * channels);
         AutoBuffer<int16_t> b1(441 * channels);
         int16_t prevEnd0 = seqEnd;
         seqEnd = Sequence(b.Get(), 441 * channels, prevEnd0);
         int16_t prevEnd1 = seqEnd;
         seqEnd = Sequence(b1.Get(), 441 * channels, seqEnd);
         ap.Input(b.Get(), 441);
         ap.Input(b1.Get(), 441);
-        int16_t* out = ap.Output();
-        int16_t* out2 = ap.Output();
-        IsSequence(out, 441 * channels, prevEnd0);
-        IsSequence(out2, 441 * channels, prevEnd1);
-        delete[] out;
-        delete[] out2;
+        std::unique_ptr<int16_t[]> out(ap.Output());
+        std::unique_ptr<int16_t[]> out2(ap.Output());
+        IsSequence(std::move(out), 441 * channels, prevEnd0);
+        IsSequence(std::move(out2), 441 * channels, prevEnd1);
       }
     }
     // Input/output buffer size not aligned on the packet size,
     // alternating two Input and Output calls.
     {
       AudioPacketizer<int16_t, int16_t> ap(441, channels);
       int16_t prevEnd = 0;
       int16_t prevSeq = 0;
       for (int16_t i = 0; i < 10; i++) {
         AutoBuffer<int16_t> b(480 * channels);
         AutoBuffer<int16_t> b1(480 * channels);
         prevSeq = Sequence(b.Get(), 480 * channels, prevSeq);
         prevSeq = Sequence(b1.Get(), 480 * channels, prevSeq);
         ap.Input(b.Get(), 480);
         ap.Input(b1.Get(), 480);
-        int16_t* out = ap.Output();
-        int16_t* out2 = ap.Output();
-        IsSequence(out, 441 * channels, prevEnd);
+        std::unique_ptr<int16_t[]> out(ap.Output());
+        std::unique_ptr<int16_t[]> out2(ap.Output());
+        IsSequence(std::move(out), 441 * channels, prevEnd);
         prevEnd += 441 * channels;
-        IsSequence(out2, 441 * channels, prevEnd);
+        IsSequence(std::move(out2), 441 * channels, prevEnd);
         prevEnd += 441 * channels;
-        delete[] out;
-        delete[] out2;
       }
       printf("Available: %d\n", ap.PacketsAvailable());
     }
 
     // "Real-life" test case: streaming a sine wave through a packetizer, and
     // checking that we have the right output.
     // 128 is, for example, the size of a Web Audio API block, and 441 is the
     // size of a webrtc.org packet when the sample rate is 44100 (10ms)
@@ -146,22 +141,21 @@ TEST(AudioPacketizer, Test)
           for (int32_t c = 0; c < channels; c++) {
             // int16_t sinewave at 440Hz/44100Hz sample rate
             b.Get()[j * channels + c] = (2 << 14) * sine(phase);
           }
           phase++;
         }
         ap.Input(b.Get(), 128);
         while (ap.PacketsAvailable()) {
-          int16_t* packet = ap.Output();
+          std::unique_ptr<int16_t[]> packet(ap.Output());
           for (uint32_t k = 0; k < ap.PacketSize(); k++) {
             for (int32_t c = 0; c < channels; c++) {
               ASSERT_TRUE(packet[k * channels + c] ==
                           static_cast<int16_t>(((2 << 14) * sine(outPhase))));
             }
             outPhase++;
           }
-          delete [] packet;
         }
       }
     }
   }
 }