Skip to content

Commit

Permalink
fix: Fixed two memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed Nov 5, 2022
1 parent 7caf512 commit 0448fe8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
android:appCategory="audio"
tools:targetApi="s">

<profileable android:shell="true"/>

<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="${crashlyticsCollectionEnabled}" />
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/libjamesdsp-wrapper/JamesDspWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Java_me_timschneeberger_rootlessjamesdsp_interop_JamesDspWrapper_free(JNIEnv *en
setStdOutHandler(nullptr, nullptr);

JamesDSPFree(dsp);
free(dsp);
wrapper->dsp = nullptr;

JamesDSPGlobalMemoryDeallocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class MainApplication : Application(), SharedPreferences.OnSharedPreferenceChang
}

override fun onTrimMemory(level: Int) {
Timber.w("onTrimMemory: Memory trim at level $level requested")
if (level >= 60)
Timber.w("onTrimMemory: Memory trim at level $level requested")
FirebaseCrashlytics.getInstance().setCustomKey("last_memory_trim_event", SimpleDateFormat("yyyyMMdd HHmmss z", Locale.US).format(Date()))
FirebaseCrashlytics.getInstance().setCustomKey("last_memory_trim_level", level)
super.onTrimMemory(level)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import timber.log.Timber

class PreferenceGroupFragment : PreferenceFragmentCompat() {
private val eelParser = EelParser()
private var recyclerView: RecyclerView? = null

private val listener =
SharedPreferences.OnSharedPreferenceChangeListener { _, _ ->
Expand Down Expand Up @@ -75,11 +76,20 @@ class PreferenceGroupFragment : PreferenceFragmentCompat() {
fun updateLiveprog(newValue: String) {
eelParser.load(newValue)
val count = eelParser.properties.count()
liveprogParams?.isEnabled = count > 0
liveprogParams?.summary = if(count > 0)
requireContext().resources.getQuantityString(R.plurals.custom_parameters, count, count)
val uiUpdate = {
liveprogParams?.isEnabled = count > 0
liveprogParams?.summary = if(count > 0)
requireContext().resources.getQuantityString(R.plurals.custom_parameters, count, count)
else
getString(R.string.liveprog_additional_params_not_supported)
}

if (recyclerView == null)
// Recycler view doesn't exist yet, directly setup the preference
uiUpdate()
else
getString(R.string.liveprog_additional_params_not_supported)
// Recycler view does exist, queue on UI thread
recyclerView!!.post(uiUpdate)
}

liveprogFile?.summaryProvider = SummaryProvider<FileLibraryPreference> {
Expand All @@ -88,10 +98,6 @@ class PreferenceGroupFragment : PreferenceFragmentCompat() {
"No script selected"
else
eelParser.description
/*if(eelParser.hasDescription)
eelParser.description + " ("+ eelParser.fileName +")"
else
eelParser.description*/
}

if(liveprogFile != null) {
Expand All @@ -103,7 +109,6 @@ class PreferenceGroupFragment : PreferenceFragmentCompat() {
true
}


liveprogParams?.setOnPreferenceClickListener {
val intent = Intent(requireContext(), LiveprogParamsActivity::class.java)
intent.putExtra(LiveprogParamsActivity.EXTRA_TARGET_FILE, liveprogFile?.value)
Expand All @@ -128,10 +133,10 @@ class PreferenceGroupFragment : PreferenceFragmentCompat() {
parent: ViewGroup,
savedInstanceState: Bundle?,
): RecyclerView {
val recyclerView = super.onCreateRecyclerView(inflater, parent, savedInstanceState)
recyclerView.itemAnimator = null // Fix to prevent RecyclerView crash if group is toggled rapidly
recyclerView.isNestedScrollingEnabled = false
return recyclerView
recyclerView = super.onCreateRecyclerView(inflater, parent, savedInstanceState)
recyclerView!!.itemAnimator = null // Fix to prevent RecyclerView crash if group is toggled rapidly
recyclerView!!.isNestedScrollingEnabled = false
return recyclerView!!
}

override fun onCreateAdapter(preferenceScreen: PreferenceScreen): RecyclerView.Adapter<*> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package me.timschneeberger.rootlessjamesdsp.liveprog

import me.timschneeberger.rootlessjamesdsp.utils.md5
import timber.log.Timber
import java.io.File
import java.io.FileNotFoundException
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.floor

class EelParser {
var isFileLoaded: Boolean
Expand All @@ -23,6 +23,7 @@ class EelParser {
private set
var properties = ArrayList<EelBaseProperty>()
private set
private var lastFileHash: ByteArray? = null

fun load(path: String): Boolean {
if(path.isBlank()) {
Expand All @@ -39,13 +40,19 @@ class EelParser {
this.path = null
return false
}
properties = arrayListOf()

Timber.d("Loaded '$path'")

if(lastFileHash != null && lastFileHash.contentEquals(contents?.md5)) {
Timber.w("Parsing skipped. Script identical with previous file.")
return false
}

// Parse description
parseDescription()

properties = arrayListOf()

// Parse number range parameters
val rangeParamRegex = """(?<var>\w+):(?<def>-?\d+\.?\d*)?<(?<min>-?\d+\.?\d*),(?<max>-?\d+\.?\d*),?(?<step>-?\d+\.?\d*)?>(?<desc>[\s\S][^\n]*)""".toRegex()
contents!!.lines().forEach next@ {
Expand Down Expand Up @@ -89,6 +96,7 @@ class EelParser {
}
}

lastFileHash = contents?.md5
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ import android.os.Bundle
import android.os.Parcelable
import android.text.Html
import android.text.Spanned
import android.util.Log
import androidx.appcompat.view.ContextThemeWrapper
import me.timschneeberger.hiddenapi_impl.BuildConfig
import me.timschneeberger.rootlessjamesdsp.R
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import java.io.Serializable
import java.math.BigDecimal
import java.math.RoundingMode
import java.security.MessageDigest
import java.util.*
import kotlin.math.*

Expand Down Expand Up @@ -84,3 +79,8 @@ inline fun <reified T : Parcelable> Bundle.getParcelableAs(key: String): T? {
}

fun Boolean.toShort() = (if (this) 1 else 0).toShort()

val String.md5: ByteArray
get() {
return MessageDigest.getInstance("MD5").digest(this.toByteArray())
}

0 comments on commit 0448fe8

Please sign in to comment.