Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f8d240eb authored by Chih-hung Hsieh's avatar Chih-hung Hsieh Committed by Gerrit Code Review
Browse files

Merge "Fix potential memory leaks"

parents 9bbbdf49 b7983492
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include <jni.h>
#include <fstream>
#include <memory>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
@@ -42,7 +43,7 @@ extern "C" JNIEXPORT int JNICALL Java_com_android_media_benchmark_library_Native
        return -1;
    }

    Decoder *decoder = new Decoder();
    std::unique_ptr<Decoder> decoder(new (std::nothrow) Decoder());
    Extractor *extractor = decoder->getExtractor();
    if (!extractor) {
        ALOGE("Extractor creation failed");
@@ -125,6 +126,5 @@ extern "C" JNIEXPORT int JNICALL Java_com_android_media_benchmark_library_Native
        inputFp = nullptr;
    }
    extractor->deInitExtractor();
    delete decoder;
    return 0;
}
+2 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include <jni.h>
#include <fstream>
#include <memory>
#include <string>
#include <sys/stat.h>

@@ -47,7 +48,7 @@ extern "C" JNIEXPORT int32_t JNICALL Java_com_android_media_benchmark_library_Na
        return MUXER_OUTPUT_FORMAT_INVALID;
    }

    Muxer *muxerObj = new Muxer();
    std::unique_ptr<Muxer> muxerObj(new (std::nothrow) Muxer());
    Extractor *extractor = muxerObj->getExtractor();
    if (!extractor) {
        ALOGE("Extractor creation failed");
@@ -159,7 +160,6 @@ extern "C" JNIEXPORT int32_t JNICALL Java_com_android_media_benchmark_library_Na
    }
    env->ReleaseStringUTFChars(jFormat, fmt);
    extractor->deInitExtractor();
    delete muxerObj;

    return 0;
}
+12 −11
Original line number Diff line number Diff line
@@ -17,11 +17,13 @@
//#define LOG_NDEBUG 0
#define LOG_TAG "C2Encoder"

#include <memory>

#include "C2Encoder.h"

int32_t C2Encoder::createCodec2Component(string compName, AMediaFormat *format) {
    ALOGV("In %s", __func__);
    mListener.reset(new CodecListener(
    mListener.reset(new (std::nothrow) CodecListener(
            [this](std::list<std::unique_ptr<C2Work>> &workItems) { handleWorkDone(workItems); }));
    if (!mListener) return -1;

@@ -125,7 +127,7 @@ int32_t C2Encoder::encodeFrames(ifstream &eleStream, size_t inputBufferSize) {
    }
    int32_t numFrames = (inputBufferSize + frameSize - 1) / frameSize;
    // Temporary buffer to read data from the input file
    char *data = (char *)malloc(frameSize);
    std::unique_ptr<char[]> data(new (std::nothrow) char[frameSize]);
    if (!data) {
        ALOGE("Insufficient memory to read from input file");
        return -1;
@@ -169,7 +171,7 @@ int32_t C2Encoder::encodeFrames(ifstream &eleStream, size_t inputBufferSize) {
        if (inputBufferSize - offset < frameSize) {
            frameSize = inputBufferSize - offset;
        }
        eleStream.read(data, frameSize);
        eleStream.read(data.get(), frameSize);
        if (eleStream.gcount() != frameSize) {
            ALOGE("read() from file failed. Incorrect bytes read");
            return -1;
@@ -191,8 +193,8 @@ int32_t C2Encoder::encodeFrames(ifstream &eleStream, size_t inputBufferSize) {
                    return view.error();
                }

                memcpy(view.base(), data, frameSize);
                work->input.buffers.emplace_back(new LinearBuffer(block));
                memcpy(view.base(), data.get(), frameSize);
                work->input.buffers.emplace_back(new (std::nothrow) LinearBuffer(block));
            } else {
                std::shared_ptr<C2GraphicBlock> block;
                status = mGraphicPool->fetchGraphicBlock(
@@ -211,16 +213,16 @@ int32_t C2Encoder::encodeFrames(ifstream &eleStream, size_t inputBufferSize) {
                uint8_t *pY = view.data()[C2PlanarLayout::PLANE_Y];
                uint8_t *pU = view.data()[C2PlanarLayout::PLANE_U];
                uint8_t *pV = view.data()[C2PlanarLayout::PLANE_V];
                memcpy(pY, data, mWidth * mHeight);
                memcpy(pU, data + mWidth * mHeight, (mWidth * mHeight >> 2));
                memcpy(pV, data + (mWidth * mHeight * 5 >> 2), mWidth * mHeight >> 2);
                work->input.buffers.emplace_back(new GraphicBuffer(block));
                memcpy(pY, data.get(), mWidth * mHeight);
                memcpy(pU, data.get() + mWidth * mHeight, (mWidth * mHeight >> 2));
                memcpy(pV, data.get() + (mWidth * mHeight * 5 >> 2), mWidth * mHeight >> 2);
                work->input.buffers.emplace_back(new (std::nothrow) GraphicBuffer(block));
            }
            mStats->addFrameSize(frameSize);
        }

        work->worklets.clear();
        work->worklets.emplace_back(new C2Worklet);
        work->worklets.emplace_back(new (std::nothrow) C2Worklet);

        std::list<std::unique_ptr<C2Work>> items;
        items.push_back(std::move(work));
@@ -234,7 +236,6 @@ int32_t C2Encoder::encodeFrames(ifstream &eleStream, size_t inputBufferSize) {
        numFrames--;
        mNumInputFrame++;
    }
    free(data);
    return status;
}

+8 −9
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <fstream>
#include <iostream>
#include <limits>
#include <memory>

#include "BenchmarkTestEnvironment.h"
#include "C2Decoder.h"
@@ -50,7 +51,7 @@ class C2DecoderTest : public ::testing::TestWithParam<pair<string, string>> {
};

void C2DecoderTest::setupC2DecoderTest() {
    mDecoder = new C2Decoder();
    mDecoder = new (std::nothrow) C2Decoder();
    ASSERT_NE(mDecoder, nullptr) << "C2Decoder creation failed";

    int32_t status = mDecoder->setupCodec2();
@@ -66,7 +67,7 @@ TEST_P(C2DecoderTest, Codec2Decode) {
    FILE *inputFp = fopen(inputFile.c_str(), "rb");
    ASSERT_NE(inputFp, nullptr) << "Unable to open " << inputFile << " file for reading";

    Extractor *extractor = new Extractor();
    std::unique_ptr<Extractor> extractor(new (std::nothrow) Extractor());
    ASSERT_NE(extractor, nullptr) << "Extractor creation failed";

    // Read file properties
@@ -85,7 +86,7 @@ TEST_P(C2DecoderTest, Codec2Decode) {
        int32_t status = extractor->setupTrackFormat(curTrack);
        ASSERT_EQ(status, 0) << "Track Format invalid";

        uint8_t *inputBuffer = (uint8_t *)malloc(fileSize);
        std::unique_ptr<uint8_t[]> inputBuffer(new (std::nothrow) uint8_t[fileSize]);
        ASSERT_NE(inputBuffer, nullptr) << "Insufficient memory";

        vector<AMediaCodecBufferInfo> frameInfo;
@@ -100,7 +101,7 @@ TEST_P(C2DecoderTest, Codec2Decode) {
            // copy the meta data and buffer to be passed to decoder
            ASSERT_LE(inputBufferOffset + info.size, fileSize) << "Memory allocated not sufficient";

            memcpy(inputBuffer + inputBufferOffset, csdBuffer, info.size);
            memcpy(inputBuffer.get() + inputBufferOffset, csdBuffer, info.size);
            frameInfo.push_back(info);
            inputBufferOffset += info.size;
            idx++;
@@ -113,7 +114,7 @@ TEST_P(C2DecoderTest, Codec2Decode) {
            // copy the meta data and buffer to be passed to decoder
            ASSERT_LE(inputBufferOffset + info.size, fileSize) << "Memory allocated not sufficient";

            memcpy(inputBuffer + inputBufferOffset, extractor->getFrameBuf(), info.size);
            memcpy(inputBuffer.get() + inputBufferOffset, extractor->getFrameBuf(), info.size);
            frameInfo.push_back(info);
            inputBufferOffset += info.size;
        }
@@ -127,7 +128,7 @@ TEST_P(C2DecoderTest, Codec2Decode) {
                ASSERT_EQ(status, 0) << "Create component failed for " << codecName;

                // Send the inputs to C2 Decoder and wait till all buffers are returned.
                status = mDecoder->decodeFrames(inputBuffer, frameInfo);
                status = mDecoder->decodeFrames(inputBuffer.get(), frameInfo);
                ASSERT_EQ(status, 0) << "Decoder failed for " << codecName;

                mDecoder->waitOnInputConsumption();
@@ -141,10 +142,8 @@ TEST_P(C2DecoderTest, Codec2Decode) {
                mDecoder->resetDecoder();
            }
        }
        free(inputBuffer);
        fclose(inputFp);
        extractor->deInitExtractor();
        delete extractor;
        delete mDecoder;
        mDecoder = nullptr;
    }
@@ -174,7 +173,7 @@ INSTANTIATE_TEST_SUITE_P(
                          make_pair("crowd_1920x1080_25fps_4000kbps_h265.mkv", "hevc")));

int main(int argc, char **argv) {
    gEnv = new BenchmarkTestEnvironment();
    gEnv = new (std::nothrow) BenchmarkTestEnvironment();
    ::testing::AddGlobalTestEnvironment(gEnv);
    ::testing::InitGoogleTest(&argc, argv);
    int status = gEnv->initFromOptions(argc, argv);
+8 −8
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <fstream>
#include <iostream>
#include <limits>
#include <memory>

#include "BenchmarkTestEnvironment.h"
#include "C2Encoder.h"
@@ -50,7 +51,7 @@ class C2EncoderTest : public ::testing::TestWithParam<pair<string, string>> {
};

void C2EncoderTest::setupC2EncoderTest() {
    mEncoder = new C2Encoder();
    mEncoder = new (std::nothrow) C2Encoder();
    ASSERT_NE(mEncoder, nullptr) << "C2Encoder creation failed";

    int32_t status = mEncoder->setupCodec2();
@@ -66,7 +67,7 @@ TEST_P(C2EncoderTest, Codec2Encode) {
    FILE *inputFp = fopen(inputFile.c_str(), "rb");
    ASSERT_NE(inputFp, nullptr) << "Unable to open input file for reading";

    Decoder *decoder = new Decoder();
    std::unique_ptr<Decoder> decoder(new (std::nothrow) Decoder());
    ASSERT_NE(decoder, nullptr) << "Decoder creation failed";

    Extractor *extractor = decoder->getExtractor();
@@ -88,7 +89,7 @@ TEST_P(C2EncoderTest, Codec2Encode) {
        int32_t status = extractor->setupTrackFormat(curTrack);
        ASSERT_EQ(status, 0) << "Track Format invalid";

        uint8_t *inputBuffer = (uint8_t *)malloc(fileSize);
        std::unique_ptr<uint8_t[]> inputBuffer(new (std::nothrow) uint8_t[fileSize]);
        ASSERT_NE(inputBuffer, nullptr) << "Insufficient memory";

        vector<AMediaCodecBufferInfo> frameInfo;
@@ -102,7 +103,7 @@ TEST_P(C2EncoderTest, Codec2Encode) {
            // copy the meta data and buffer to be passed to decoder
            ASSERT_LE(inputBufferOffset + info.size, fileSize) << "Memory allocated not sufficient";

            memcpy(inputBuffer + inputBufferOffset, extractor->getFrameBuf(), info.size);
            memcpy(inputBuffer.get() + inputBufferOffset, extractor->getFrameBuf(), info.size);
            frameInfo.push_back(info);
            inputBufferOffset += info.size;
        }
@@ -114,7 +115,8 @@ TEST_P(C2EncoderTest, Codec2Encode) {
                                  << " for dumping decoder's output";

        decoder->setupDecoder();
        status = decoder->decode(inputBuffer, frameInfo, decName, false /*asyncMode */, outFp);
        status = decoder->decode(inputBuffer.get(), frameInfo, decName,
                                 false /*asyncMode */, outFp);
        ASSERT_EQ(status, AMEDIA_OK) << "Decode returned error : " << status;

        // Encode the given input stream for all C2 codecs supported by device
@@ -149,11 +151,9 @@ TEST_P(C2EncoderTest, Codec2Encode) {
        // Destroy the decoder for the given input
        decoder->deInitCodec();
        decoder->resetDecoder();
        free(inputBuffer);
    }
    fclose(inputFp);
    extractor->deInitExtractor();
    delete decoder;
    delete mEncoder;
    mEncoder = nullptr;
}
@@ -176,7 +176,7 @@ INSTANTIATE_TEST_SUITE_P(
                          make_pair("crowd_1920x1080_25fps_4000kbps_h265.mkv", "hevc")));

int main(int argc, char **argv) {
    gEnv = new BenchmarkTestEnvironment();
    gEnv = new (std::nothrow) BenchmarkTestEnvironment();
    ::testing::AddGlobalTestEnvironment(gEnv);
    ::testing::InitGoogleTest(&argc, argv);
    int status = gEnv->initFromOptions(argc, argv);
Loading