Under Review
over 1 year ago

"import Rez" or "using Rez" breaks background resources

I started with the BackgroundTimer sample project, and added a println to its BackgroundTimerServiceDelegate.initialize:

    public function initialize() {
        ServiceDelegate.initialize();
        System.println(Application.loadResource(Rez.Strings.TimerExpired));
    }

Now if I build and run the app, it prints out

Background: Your timer has expired!

(thats the expected output of the println) 

But if I now add "import Rez" or "using Rez" to the top of the file, I get

Error: Illegal Access (Out of Bounds)

Details: Failed invoking <symbol>

Stack:

- initialize() at ./BackgroundTimer/source/BackgroundTimerBackground.mc:23 0x1000027a

- getServiceDelegate() at ./BackgroundTimer/source/BackgroundTimerApp.mc:64 0x10000042

Looking at Rez.mcgen, and the compiler's output at -g, it looks like background code is supposed to access BackgroundRez.*, and that the runtime somehow binds Rez to BackgroundRez while background code is running.

The problem, then, seems to be that "using Rez;" binds Rez at compile time, and so the background code is actually trying to access the foreground resources.

In fact, I can fix it by adding "using BackgroundRez as Rez" instead - but since BackgroundRez seems to be just an implementation detail (ie its not documented anywhere), it seems like a risky thing to rely on. Also, there could be shared code that runs in both the foreground and background - so its not necessarily clear which one to use...

And the *reason* for that "using Rez" is that Rez.Strings.foo without a using generates 20 bytes of code (and $.Rez.Strings.foo generates 26). But with the using its only 18. Its also faster because it looks up the Rez module directly.

Parents
  • add scope

    Its already got scope="background" (as you could have easily checked)

        <string id="TimerExpired" scope="background">Your timer has expired!</string>
    

    Besides - how could the initial version (without "import Rez;") have worked if it wasn't correctly scoped?

    To be clear. The bug is that correctly scoped background resources, that work perfectly without "import Rez;" fail when you add "import Rez;"

Comment
  • add scope

    Its already got scope="background" (as you could have easily checked)

        <string id="TimerExpired" scope="background">Your timer has expired!</string>
    

    Besides - how could the initial version (without "import Rez;") have worked if it wasn't correctly scoped?

    To be clear. The bug is that correctly scoped background resources, that work perfectly without "import Rez;" fail when you add "import Rez;"

Children
No Data