From 20d18966bcd1ed01b95a2643695a6aeb1afc273c Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Tue, 29 Jan 2019 19:30:40 -0800 Subject: [PATCH] media: Silence an analyzer complaint; clean up code The static analyzer is complaining about a memory leak when we create this unique pointer. This appears to just be a case of the analyzer not properly modelling returned temporaries. While I'm in the area, unique_ptr is generally an antipattern: if we instead use an empty struct with an attached operator(), the unique_ptr shrinks to sizeof(void *) bytes (instead of sizeof(void *) * 2), and the compiler no longer has to indirectly call free(). As luck would have it, this small refactor makes the analyzer stop complaining about this code. Bug: None Test: Ran the static analyzer. It's happier, and this builds. Change-Id: I0c98860e2169ceb8e9d21e577cad89d1ef2c6ff9 --- core/jni/android_media_AudioAttributes.cpp | 2 +- core/jni/android_media_AudioAttributes.h | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/jni/android_media_AudioAttributes.cpp b/core/jni/android_media_AudioAttributes.cpp index 4be4def4a87ba..b87a34d6c7aa5 100644 --- a/core/jni/android_media_AudioAttributes.cpp +++ b/core/jni/android_media_AudioAttributes.cpp @@ -135,7 +135,7 @@ JNIAudioAttributeHelper::UniqueAaPtr JNIAudioAttributeHelper::makeUnique() { audio_attributes_t *aa = new (calloc(1, sizeof(audio_attributes_t))) audio_attributes_t{AUDIO_ATTRIBUTES_INITIALIZER}; - return UniqueAaPtr{aa, free}; + return UniqueAaPtr{aa}; } jint JNIAudioAttributeHelper::nativeFromJava(JNIEnv* env, jobject jAudioAttributes, diff --git a/core/jni/android_media_AudioAttributes.h b/core/jni/android_media_AudioAttributes.h index c55835222086d..628f7e3469e8e 100644 --- a/core/jni/android_media_AudioAttributes.h +++ b/core/jni/android_media_AudioAttributes.h @@ -27,7 +27,11 @@ namespace android { class JNIAudioAttributeHelper { public: - using UniqueAaPtr = std::unique_ptr; + struct FreeDeleter { + void operator()(void *p) const { ::free(p); } + }; + + using UniqueAaPtr = std::unique_ptr; /** * @brief makeUnique helper to prevent leak