Exception locking surface - lockCanvas() returns a null?

by mcmc » Sat, 04 Apr 2009 01:32:35 GMT


Sponsored Links
 Everytime it hits the "c = mHolder.lockCanvas();" line, it pops up
with that same error mentioned below.

Also, I realize that c returns a null after called lockCanvas(), but
mHolder is still active. Why might this be?


I'm getting the following error as seen from the logcat window:

E/SurfaceComposerClient(  608): eLocked set when entering lock_layer
(), layer=1 (lcblk=0x410480a0), state=00000022
E/SurfaceHolder(  608): Exception locking surface
E/SurfaceHolder(  608): java.lang.IllegalArgumentException
E/SurfaceHolder(  608):         at
android.view.Surface.lockCanvasNative(Native Method)
E/SurfaceHolder(  608):         at android.view.Surface.lockCanvas
(Surface.java:190)
E/SurfaceHolder(  608):         at android.view.SurfaceView
$2.internalLockCanvas(SurfaceView.java:549)
E/SurfaceHolder(  608):         at android.view.SurfaceView
$2.lockCanvas(SurfaceView.java:528)


This is the basis of my code, which is primarily based on the
lunarview:

@Override
public void run() {
    while (mRun) {
        Canvas c = null;
        try {
            mHolder = getHolder();      //make sure holder is updated
            c = mHolder.lockCanvas();
            synchronized (mHolder) {
                if (mMode == STATE_RUNNING && c != null){
                    doDraw(c);
                }
            }
        } finally {
            // do this in a finally so that if an exception is thrown
            // during the above, we don't leave the Surface in an
            // inconsistent state
            if (c != null) {
                mHolder.unlockCanvasAndPost(c);
            }
        }
    }
}


Can someone get back to me ASAP, this is urgent.

Thanks a lot,
mcmc

--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by Streets Of Boston » Sat, 04 Apr 2009 01:38:48 GMT


 I don't have a direct answer for you.
But this is what i usually do when problems baffle me. I browse the
git-source of Android.

Try to find the source of SurfaceHolder and find the lockCanvas( ) and
see why it may throw an exception.



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Exception locking surface - lockCanvas() returns a null?

by ellipsoidmob...@googlemail.com » Mon, 06 Apr 2009 02:15:57 GMT


 Might also be worth checking that you aren't drawing into the surface
from a different thread, and that you aren't drawing before the
surfaceCreated() callback
--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Tue, 07 Apr 2009 02:48:24 GMT


 actually, I am drawing onto the surface from a different thread...
that's not allowed?? how would I go around doing this?

I'm not drawing before the surfaceCreated() callback...

On Apr 5, 11:15am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Tue, 07 Apr 2009 08:38:14 GMT


 i, now, combined the two threads into one, and I still get the same
error at lockCanvas().

any ideas would be much appreciated. thank you.




--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by ellipsoidmob...@googlemail.com » Tue, 07 Apr 2009 20:34:42 GMT


 Oh - I think I can see the problem in your code now. You've got your
'finally' block outside of your while loop, rather than inside it.
This means that you are calling lockCanvas() repeatedly without
calling UnlockCanvasAndPost(). The exception is going to be thrown on
the 2nd iteration of the while loop, as you are calling lockCanvas()
when the canvas is already locked.

--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Wed, 08 Apr 2009 03:25:34 GMT


 my finally is inside my while loop.
but maybe i'm not doing it correctly...
do I have to call unlockCanvasAndPost every time I call lockCanvas
(even if it returns a null)? or do I only call unlockCanvasAndPost if
the lockCanvas returns a non null canvas (which is my current
implementation)?

On Apr 7, 5:34am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Wed, 08 Apr 2009 03:28:39 GMT


 my finally is inside my while loop.
but maybe i'm not doing it correctly...
do I have to call unlockCanvasAndPost every time I call lockCanvas
(even if it returns a null)? or do I only call unlockCanvasAndPost if
the lockCanvas returns a non null canvas (which is my current
implementation)?

On Apr 7, 5:34am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by ellipsoidmob...@googlemail.com » Wed, 08 Apr 2009 19:04:46 GMT


 Sorry, I misread your code before. It looks the same as lunarview and
also the same as my game loop, both of which work fine, so I think it
must be something you are doing outside of this code snippet.

You do have to unlock for every successful lock (but not if you get a
null back), so your code looks correct to me in this regard

The error looks like the one you would get if the canvas was already
locked when you called lock, so are you sure you aren't doing
something elsewhere in the code that is locking the canvas?

Maybe you could also add some more trace info - is the unlock call
definitely being reached, and is the exception thrown the 1st time
round the loop or after a number of iterations?
--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Thu, 09 Apr 2009 02:01:29 GMT


 The exception is thrown the 1st time around the loop. And Unlock is
never called, since there was never a successful Lock in the first
place... :(

Right now, my code looks like this one:
 http://developer.android.com/guide/samples/ApiDemos/src/com/example/android/apis/graphics/GLSurfaceView.html 

However, in the run() method, instead of having:

                if ((w > 0) && (h > 0)) {
                    /* draw a frame here */
                    mRenderer.drawFrame(gl);

                    /*
                     * Once we're done with GL, we need to call
swapBuffers()
                     * to instruct the system to display the rendered
frame
                     */
                    mEglHelper.swap();
                }

I modified it a little:

                               if ((w > 0) && (h > 0)) {
                                        mRenderer.drawFrame(gl);

                                        if (mRun){
                                                Canvas c = null;
                                                try {
                                                    mHolder = getHolder();      
//make sure holder is updated
                                                    c = mHolder.lockCanvas();
                                                    synchronized (mHolder) {
                                                        if (mMode == 
STATE_RUNNING && c != null){
                                                            doDraw(c);
                                                        }
                                                    }
                                                } finally {
                                                    // do this in a finally so 
that if an exception is thrown
                                                    // during the above, we 
don't leave the Surface in an
                                                    // inconsistent state
                                                    if (c != null) {
                                                        
mHolder.unlockCanvasAndPost(c);
                                                    }
                                                }
                                        }

                                         mEglHelper.swap();
                                }


On Apr 8, 4:04am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by ellipsoidmob...@googlemail.com » Thu, 09 Apr 2009 17:32:07 GMT


 I hadn't realised you are doing openGL stuff. I've used surfaceview/
holder in the same was as the lunarlander, but I haven't used openGL
so I don't know how that works.

But, at a glance, it looks like you've got a mix of two different
approaches here - maybe the lockcanvas/unlockcanvasandpost approach is
not appropriate when using openGL as the openGL code is controlling
and locking the surface?
--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Fri, 10 Apr 2009 04:45:21 GMT


 wow thanks so much for your help. I think you're right! :D

however, now I run into another problem. Do you know how to convert
openGL to simply using the standard android/java 2D APIs?

On Apr 9, 2:31am, "ellipsoidmob...@googlemail.com"


--~--~---------~--~----~------------~-------~--~----~



Exception locking surface - lockCanvas() returns a null?

by mcmc » Sat, 11 Apr 2009 01:01:05 GMT


 thank you sooo much, everyone!
i've been meaning for someone to tell me that since a very long time
ago, but no one did, so i was stuck!

:)





--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Icon Spacing

Android 2.0: Why are the icons so far apart on the home screens?  They
could have fit another row or two of icons.  If you have the Google
search bar at the top, you can only fit 12 icons on the main screen.

--

2. Sr. SW Engineer, Java, J2ME, Android, Messaging Middleware Development - Central New Jersey and Dallas, TX

Riccione Resources, the Recruitment Firm with over 24 years
experience, is proud to announce an Embedded Messaging Developer
position.  The ideal candidate is a generalist with a strong
background in messaging software; open-minded and possesses a drive to
learn new things quickly; will work with a team of like-minded
engineers to build advanced messaging middleware components of a next
generation wireless product.

This lab has 2 major goals:
- Research and development of next generation wireless software ,
platforms and solutions for the global marketplace
- Development and commercialization of advanced products for the North
American Market to maintain competitive edge

Responsibilities
- Design and build next-generation Messaging Middleware
- Measure and characterize performance of the middleware, and suitably
improve performance as needed
- Develop abstractions to hide platform specific capabilities
- Be the in-house Guru for various forms of messaging, including
email, text messaging, instant messaging, Visual Voice Mail (VVM), and
social networking protocols
- Travel to overseas offices and customer sites (worldwide) for 1-3
weeks at a time to assist in integration, commercialization activities
- There may be 3-4 such trips per year.

Qualifications
- Senior level software development skills
- Java, J2ME and Android applications or middleware development
- Knowledge of and experience with one or more of the following:
Email protocols such as IMAP, POP3, SMTP, ActiveSync, Lotus Notes;
text messaging protocols such as SMS, MMS, and IMS; Visual Voice Mail
(VVM); instant messaging protocols; social networking APIs for
accessing services such as Facebook, MySpace, Twitter, etc.
- Very strong design and abstraction skills - as it relates to
programming, development of re-usable components etc.
- B.S. Computer Science
Desired but not required:
- Masters degree in CS
- Fluent in C/C++ programming (C++ preferred)

Salary:  $100k - 150k + Bonus Plan

Relocation:  Moving assistance

Email resume to ka...@riccione.com

--

3. Windows 7 + adb driver for G1 -- can't install, because "the best driver" is already installed. Augh.

4. Interrupting the app uninstaller

5. How to set target in eclipse at the time of creating AVD.

6. android-sdk sans gui

7. Trouble setting up SDK on ubuntu 9.10