Exception locking surface - lockCanvas() returns a null?

by Dianne Hackborn » Fri, 10 Apr 2009 06:31:04 GMT


Sponsored Links
 If you want to use the standard 2d APIs, don't use OpenGL.







-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

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



Exception locking surface - lockCanvas() returns a null?

by Stoyan Damov » Sat, 11 Apr 2009 02:28:52 GMT


 Just my 2 stotinki -- I see more and more developers using this WRONG
resource acquisition pattern with try...finally:

try
{
    acquireResource();
}
finally
{
    releaseResource();
}

This is plain wrong. The correct way to obtain a resource and release
it is to acquire it BEFORE the try block, and release it in the
finally block:

acquireResource();
try
{
    // do whatever with the resource
}
finally
{
    releaseResource();
}

if acquireResource could throw an error, the correct way to handle it
is in an outer try block:

try
{
    acquireResource();
    try
    {
        // do whatever with the resource
    }
    finally
    {
        releaseResource();
    }
}
catch (WhateverException e)
{
}

as opposed to this anti-pattern (again widely used):

Resource r = null;
try
{
    r = acquireResource();
    // do whatever with the resource
}
finally
{
    if (r != null)
    {
        releaseResource();
    }
}

Cheers







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


Sponsored Links


Other Threads

1. Using AsyncTask as Serializable or Parcelable --> To void activity destruction(orientation or during incoming phone call)

Hi Guys,

I have a requirement where I have to read/parse some data from the
server(using HTTPConnection) and then display it on to the screen. To
avoid ANR issue I used AsyncTask to perform the above. Now in order to
avoid application crash in case the activity gets destroyed(either by
OS or if orientation changes or say on any incoming call-->where user
stays on the phone for long and for some reason OS destroys/recreates
the activity). Currently, I hols the reference of AsyncTask in my
activity and then provide a callback(the activity itself) to AsyncTask
so that I can pos back message to activity(like closing progress
dialog box).In order to avoid memory leaks on onPause I nullify this
activity reference in AsyncTask.Before onPause I save the AsyncTask
instance using onSaveInstanceState method but I have to use
putSerializable for that. I have read on the forums that serialization
method is slow, so my question is should I use parcelable approach(and
whether the converting AsyncTask to parcelable will be same as
creating any other object parcelable). or both these approaches are
totally wrong i.e. I should not be using both the above approaches if
no then what is the correct approach which gives maximum performance
benefits. No I read at one forum also on android blog(and in their
examples) where they suggest storing AsyncTask
using onRetainNonConfigurationInstance() method as that gives
performance benefits but I think that approach just works only for
cases where activity gets destroyed via orienattion changes but does
not cover other scenarios where activity gets destroyed because of
things like low memory, phone calls etc.

I would really appreciate if anybody has any suggestions or ideas on
how to do it right(or if somebody can confirm that whatever I am doing
is right and I just have to choose between parcelable or
serializable?). I am able to get it working very well, its just that I
am not sure if it can cause any issue in production.

This is such a common task and there is no good example for this and
looks like people are still scratching their heads on this.

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

2. How to use PopupWindow()

How to create a pop up window????

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

3. referencing projects in eclipse

4. how to make an activty go in background

5. Can I apply git tag command to Android Source Code.

6. How can I specify a border color for an ImageView when it has focus

7. file change notification permission for native c application?