Flex: recovering from a damaged local SharedObject

My Flex uses local SharedObjects. There have been cases where the Flash cookie has been corrupted, for example due to a plugin crash. In this case, SharedObjects.getLocal will throw an exception (# 2006).

My client wants the application to recover gracefully: if the cookie is corrupted, I have to replace it with an empty one.

The problem is, if SharedObject.getLocal doesn't return a SharedObject instance, I have nothing to call clear ().

How can I delete or replace such a cookie?

Many thanks!


There is little code to display - I am accessing the local cookie and I can easily catch the exception. But how can I create a new shared object in the same location as soon as I catch the exception?

 try {
     localStorage = SharedObject.getLocal("heywoodsApp");
 } catch (err:Error) {
       // what do I do here?


The error is easily reproducible by corrupting the binary content of the Flash cookie with the editor.


source to share

2 answers

You need to test the length of the SharedObject and recreate if it is 0. Also always use flush to write to the object. Here's the function we're using to count the number of times our software runs:

private function usageNumber():void {
    usage = SharedObject.getLocal("usage");

    if (usage.size > 0) {
       var usageStr:String = usage.data.usage;      
       var usageNum:Number = parseInt(usageStr);

       usageNum = usageNum + 1; 
       usageStr = usageNum.toString();

       usage.data.usage = usageStr;


    } else {

       usage.data.usage = "1";




It is important to note that if an object is not available it will be automatically recreated. This is the confusing part about SharedObjects.

All we do is declare a variable globally:

public var usage:SharedObject;


And then calling it in the init () function:

usage = SharedObject.getLocal("usage");


If not present, it is created.



I'm not sure why you get the range - esp error if you report you can find it. My only guess as to what this is is border crossing capability regarding cross-domain policy. Assuming the IT server has control over where the server is located, if the subdomain ever changes or even has an access type (from standard to https), this can cause problems, especially if the application continues (across multiple releases). It would be hard for me to believe that you are trying to get an SO name that has already been named by another application - essentially a name clash. For that matter, many of us still use reverse dns style naming conventions even on these things.

If you can catch the error, it should be relatively trivial to recover: - just declare the variable outside of the try's scope so that it is catchable. [edit]: Since this is a static method, you may need to create a postfix to essentially start with a new identifier.

var mySO:SharedObject;
      mySO = SharedObject.getLocal('my.reversedns.so_name_temp_name');
      //might want to dispatch an error event or rethrow a specific exception
      //to alert the user their "preferences" were reset.




All Articles