voghDev/PdfViewPager

A/Bitmap: Error, cannot access an invalid/free'd bitmap here! - v1.0.6

lucandr opened this issue · 2 comments

Hello, I am getting this error when I try to close the adapter:

A/Bitmap: Error, cannot access an invalid/free'd bitmap here!

My code is:

import es.voghdev.pdfviewpager.library.RemotePDFViewPager
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter
import es.voghdev.pdfviewpager.library.remote.DownloadFile
import es.voghdev.pdfviewpager.library.util.FileUtil

class PdfViewerFragment : NavigationFragment() {

    private lateinit var binding: FragmentPdfViewerBinding

    private var pdfUrl: String? = null
    private var toolbarTitle: String? = null
    private var adapter: PDFPagerAdapter? = null

    companion object {

        private const val PARAM_PDF_URL = "paramPdfUrl"
        private const val PARAM_TOOLBAR_TITLE = "paramToolbarTitle"

        fun newInstance(pdfUrl: String, toolbarTitle: String): PdfViewerFragment {
            val fragment = PdfViewerFragment()
            val arguments = Bundle()
            arguments.putString(PARAM_PDF_URL, pdfUrl)
            arguments.putString(PARAM_TOOLBAR_TITLE, toolbarTitle)
            fragment.arguments = arguments
            return fragment
        }
    }

    override fun onAttach(context: Context) {
        OwlApplication.getApplicationComponent()
            .createPdfViewerFragmentSubcomponentBuilder()
            .pdfViewerFragmentModule(PdfViewerFragmentModule(this))
            .build()
            .inject(this)
        super.onAttach(context)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            pdfUrl = it.getString(PARAM_PDF_URL)
            toolbarTitle = it.getString(PARAM_TOOLBAR_TITLE)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_pdf_viewer, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.toolbar.apply {
            setNavigationIcon(R.drawable.icon_back)
            setNavigationOnClickListener {
                popFragment()
            }
        }

        activity?.let {
            RemotePDFViewPager(it, pdfUrl, object : DownloadFile.Listener {
                override fun onSuccess(url: String?, destinationPath: String?) {
                    adapter =
                        PDFPagerAdapter(it, FileUtil.extractFileNameFromURL(url))
                    binding.pdfViewPager.adapter = adapter
                }

                override fun onFailure(e: Exception?) {
                }

                override fun onProgressUpdate(progress: Int, total: Int) {
                }
            })
        }
    }

    override fun onDestroy() {
        adapter?.close()
        super.onDestroy()
    }
}

The view:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/root_lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:colorBackground"
        android:orientation="vertical"
        android:clickable="true"
        android:focusable="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/industry_bold"
                android:text="@string/earn_rewards_login_cards"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textSize="@dimen/toolbar_title_text_size" />

        </android.support.v7.widget.Toolbar>

        <es.voghdev.pdfviewpager.library.PDFViewPager
            android:id="@+id/pdf_view_pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</layout>

I am getting the same issue. Is it relevant to close the adapter after the destroy the view?

I solved this using a lifecycle observer in a custom view. It is not the best solution but I have not received this issue anymore.