square / sqlbrite

A lightweight wrapper around SQLiteOpenHelper which introduces reactive stream semantics to SQL operations.
https://square.github.io/sqlbrite/3.x/sqlbrite/
Apache License 2.0
4.57k stars 419 forks source link

Not sure where StrictMode complaining on #207

Closed iNoles closed 7 years ago

iNoles commented 7 years ago
A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                                              java.lang.Throwable: Explicit termination method 'close' not called
                                                                                  at dalvik.system.CloseGuard.open(CloseGuard.java:180)
                                                                                  at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:809)
                                                                                  at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
                                                                                  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
                                                                                  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:652)
                                                                                  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:289)
                                                                                  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                                  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                                  at com.squareup.sqlbrite2.BriteDatabase.getReadableDatabase(BriteDatabase.java:160)
                                                                                  at com.squareup.sqlbrite2.BriteDatabase$DatabaseQuery.run(BriteDatabase.java:795)
                                                                                  at com.squareup.sqlbrite2.QueryToListOperator$MappingObserver.onNext(QueryToListOperator.java:54)
                                                                                  at com.squareup.sqlbrite2.QueryToListOperator$MappingObserver.onNext(QueryToListOperator.java:39)
                                                                                  at io.reactivex.internal.observers.DisposableLambdaObserver.onNext(DisposableLambdaObserver.java:58)
                                                                                  at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:200)
                                                                                  at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
                                                                                  at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
                                                                                  at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                  at java.lang.Thread.run(Thread.java:761)

my code was just

database.createQuery("item", "SELECT * FROM item ORDER BY date DESC")
                .mapToList { News(
                        it.getString(it.getColumnIndexOrThrow("title")),
                        dateFormat.format(Date(it.getLong(it.getColumnIndexOrThrow("date")))),
                        it.getString(it.getColumnIndexOrThrow("link")),
                        it.getString(it.getColumnIndexOrThrow("image"))
                ) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        {
                            adapter.mValues = it
                            adapter.notifyDataSetChanged()
                        },
                        { Log.e(LOG_TAG, Log.getStackTraceString(it)) }
                )

Is there is something missing?

JakeWharton commented 7 years ago

Are you leaking the database object? It's hard to know without a reproducing sample.

On Sun, Jul 30, 2017, 5:11 AM Jonathan notifications@github.com wrote:

A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. java.lang.Throwable: Explicit termination method 'close' not called at dalvik.system.CloseGuard.open(CloseGuard.java:180) at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:809) at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696) at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:652) at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:289) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187) at com.squareup.sqlbrite2.BriteDatabase.getReadableDatabase(BriteDatabase.java:160) at com.squareup.sqlbrite2.BriteDatabase$DatabaseQuery.run(BriteDatabase.java:795) at com.squareup.sqlbrite2.QueryToListOperator$MappingObserver.onNext(QueryToListOperator.java:54) at com.squareup.sqlbrite2.QueryToListOperator$MappingObserver.onNext(QueryToListOperator.java:39) at io.reactivex.internal.observers.DisposableLambdaObserver.onNext(DisposableLambdaObserver.java:58) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:200) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252) at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61) at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761)

my code was just

database.createQuery("item", "SELECT * FROM item ORDER BY date DESC") .mapToList { News( it.getString(it.getColumnIndexOrThrow("title")), dateFormat.format(Date(it.getLong(it.getColumnIndexOrThrow("date")))), it.getString(it.getColumnIndexOrThrow("link")), it.getString(it.getColumnIndexOrThrow("image")) ) } .observeOn(AndroidSchedulers.mainThread()) .subscribe( { adapter.mValues = it adapter.notifyDataSetChanged() }, { Log.e(LOG_TAG, Log.getStackTraceString(it)) } )

Is there is something missing?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/square/sqlbrite/issues/207, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEerLLf6XW34bisDr_IMYSTIBF52Zks5sS_RTgaJpZM4Onhl3 .

iNoles commented 7 years ago

do you want the whole class? I can get that errors from my phone.

JakeWharton commented 7 years ago

Are you holding a reference to the subscription?

On Sun, Jul 30, 2017, 5:25 AM Jonathan notifications@github.com wrote:

do you want the whole class? I can get that errors from my phone.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/square/sqlbrite/issues/207#issuecomment-318875383, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEXoUVt9CVrogz2x4iWQ0DbACbWc2ks5sS_engaJpZM4Onhl3 .

iNoles commented 7 years ago
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val adapter = MyNewsRecyclerViewAdapter({
            launchBrowserURL(context, it.link)
        })
        list.adapter = adapter

        val dateFormat = DateUtils("EEEE, MMM d, yyyy h:mm a")

        database.createQuery("item", "SELECT * FROM item ORDER BY date DESC")
                .mapToList { News(
                        it.getString(it.getColumnIndexOrThrow("title")),
                        dateFormat.format(Date(it.getLong(it.getColumnIndexOrThrow("date")))),
                        it.getString(it.getColumnIndexOrThrow("link")),
                        it.getString(it.getColumnIndexOrThrow("image"))
                ) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        {
                            adapter.mValues = it
                            adapter.notifyDataSetChanged()
                        },
                        { Log.e(LOG_TAG, Log.getStackTraceString(it)) }
                )
    }

Not that I known of.

JakeWharton commented 7 years ago

You need to hold onto the subscription or you're leaking a listener which holds a reference to the DB.

On Sun, Jul 30, 2017, 6:21 AM Jonathan notifications@github.com wrote:

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState)

    val adapter = MyNewsRecyclerViewAdapter({
        launchBrowserURL(context, it.link)
    })
    list.adapter = adapter

    val dateFormat = DateUtils("EEEE, MMM d, yyyy h:mm a")

    database.createQuery("item", "SELECT * FROM item ORDER BY date DESC")
            .mapToList { News(
                    it.getString(it.getColumnIndexOrThrow("title")),
                    dateFormat.format(Date(it.getLong(it.getColumnIndexOrThrow("date")))),
                    it.getString(it.getColumnIndexOrThrow("link")),
                    it.getString(it.getColumnIndexOrThrow("image"))
            ) }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    {
                        adapter.mValues = it
                        adapter.notifyDataSetChanged()
                    },
                    { Log.e(LOG_TAG, Log.getStackTraceString(it)) }
            )
}

Not that I known of.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/square/sqlbrite/issues/207#issuecomment-318877302, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEWUnSxOzmWSuKEAz0-ALDIQuUEGFks5sTATjgaJpZM4Onhl3 .