1

There is some mysterious problem with creating ScriptC object while using RenderScript. Here's my code:

public class RenderScriptActivity {

    private RenderScript mRS;
    private ScriptC_kernel mScript = null;

    private Allocation m1Allocation, m2Allocation,
            m3Allocation, m4Allocation;

    private Bitmap mBitmap;

    private ImageView mView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_renderscript);
        mView = (ImageView) findViewById(R.id.image_view);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initRenderScript();
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseRenderScript();
    }

    private void initRenderScript() {
        mRS = RenderScript.create(this);

        int size = 320*240;
        m1Allocation = Allocation.createSized(mRS, Element.I16(mRS), size);
        m2Allocation = Allocation.createSized(mRS, Element.I16_4(mRS), size);
        m3Allocation = Allocation.createSized(mRS, Element.I16_4(mRS), size);
        mBitmap = Bitmap.createBitmap(1280, 720, Bitmap.Config.ARGB_8888);
        m4Allocation = Allocation.createFromBitmap(mRS, mBitmap);

        mScript = new ScriptC_kernel(mRS, getResources(), R.raw.kernel);
    }

    private void releaseRenderScript() {

        m1Allocation.destroy();
        m2Allocation.destroy();
        m3Allocation.destroy();
        m4Allocation.destroy();
//        mScript.destroy();

        mRS.finish();
    }

}

And my kernel (yes, it is empty):

#pragma version(1)
#pragma rs java_package_name(com.example.android.myrenderscript)

The problem is, that after 3rd invocation of initRenderScript() function (i.e. resuming app, pausing, resuming, pausing and resuming again), my application crashes (SIGSEV) with error:

backtrace:
#00  pc 00027034  /system/lib/libRS.so (android::renderscript::rsrClearObject(android::renderscript::Context const*, android::renderscript::ObjectBase**)+3)
#01  pc 00012c3b  /system/lib/libRSDriver.so
#02  pc 00000668  <unknown>

Additionally, if I use mScript.destory() method (which is commented above), my application crashes right after first invocation of initRenderScript(). On top of that, if I just comment line, which creates ScriptC_kernel object, everyting works fine. That's why I suppose, it is problem with creating ScriptC object.

Is there a chance, anybody knows why this problem occurs?

What is curious here, that problem happens only if I build application from command line (with ant debug command, Ubuntu 14.04). When I build it with Android Studio or Eclipse, everything works fine.

I should also mention, that in fact not application itself crashes (it contains handful of activities), but one thread, that I suppose is RenderScript's. After the crash, previous activity is brought to foreground again.

I'd greatly appreciate any help, and thanks in advance

Szał Pał
  • 306
  • 1
  • 7
  • 20
  • shouldn't `mRS.finish()` would call before destroying allocations? *Wait for any pending asynchronous opeations (such as copies to a RS allocation or RS script executions) to complete* ... lets assume that there is some pending asyc operation and you destroy allocation ... what whould happend if this operation finishes? ... also I see another problem with this code ... you are creating bitmap every resume ... and not release it ... it would end with OOMemory exception sooner or later – Selvin Jan 05 '16 at 15:45

2 Answers2

1

Are you really doing nothing else with RS here? Is it possible that your ant commands are not packaging the resources properly in your .apk? Perhaps it is worth inspecting the actual .apk file produced under each build and diffing the resources, etc. to ensure they are equivalent.

One last question: is your file called kernel.rs?

mScript = new ScriptC_kernel(mRS, getResources(), R.raw.kernel);

should really be rewritten for safety as:

mScript = new ScriptC_kernel(mRS);

They are equivalent (assuming you are using kernel.rs), and the second one is far less error-prone (in case you copy paste this line for a different Script. I could see this error happening if you load the wrong Script, but even that isn't likely.

Stephen Hines
  • 2,612
  • 1
  • 13
  • 12
-2

I have a similar problem. In my case the crash occurs at rs.destroy

Log.d(App.LOG_TAG, "deinigRenderScript 1");
mRS.finish();
Log.d(App.LOG_TAG, "deinigRenderScript 2");
mScript.destroy();
Log.d(App.LOG_TAG, "deinigRenderScript 3");
mAllocation.destroy();
Log.d(App.LOG_TAG, "deinigRenderScript 4");
mRS.destroy(); // << crash here (different tid)
Log.i(App.LOG_TAG, "deinigRenderScript 5");

In my case this helps:

$ cat project.properties  | grep render
renderscript.target=18
renderscript.opt.level=O3
renderscript.support.mode=false
renderscript.debug.opt.level=O3
renderscript.release.opt.level=O3