Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
belltown
Roku Guru

Reading events with 'for each' crashes Roku

If I run the following code to display a default springboard screen and the only thing I do after entering the channel is press the Home key on my Roku remote, then my Roku reboots:

sub main ()

port = CreateObject ("roMessagePort")
screen = CreateObject ("roSpringboardScreen")
screen.SetMessagePort (port)
screen.SetContent ({ContentType: "episode"})
screen.Show ()

for each msg in port
print "Message received"
end for

end sub


However, if I run the following code replacing the for-each loop with a while loop, then when I press the Home key the channel correctly exits:

sub main ()

port = CreateObject ("roMessagePort")
screen = CreateObject ("roSpringboardScreen")
screen.SetMessagePort (port)
screen.SetContent ({ContentType: "episode"})
screen.Show ()

while true
msg = wait (0, port)
print "Message received"
end while

end sub


According to the BrightScript reference manual, section 4.7 Events, on page 20, these two statements should be equivalent.

Any idea why the first one would crash the Roku? I'm running the 3.0 build 2227 firmware on a Roku HD model 2000C.
0 Kudos
4 REPLIES 4
renojim
Community Streaming Expert

Re: Reading events with 'for each' crashes Roku

It's probably got something to do with the Home key spewing invalid messages to the message port. Do you see a bunch of "Message received" strings when you run your second example? (I do when I try it.)

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
belltown
Roku Guru

Re: Reading events with 'for each' crashes Roku

"renojim" wrote:
It's probably got something to do with the Home key spewing invalid messages to the message port. Do you see a bunch of "Message received" strings when you run your second example? (I do when I try it.)

-JT


Yes, I do see a bunch of "Message received" strings in the second example, and only one in the first case before it crashes.

Still, I don't see why the Roku would crash in the first case if the for-each and while loops are supposed to be equivalent ways to read event messages. Does this seem like a firmware bug or a "feature"? Out of curiousity, renojim, did the first example also crash your Roku?
0 Kudos
YungBlood
Streaming Star

Re: Reading events with 'for each' crashes Roku

I did a test, with very interesting results. Although I did not use your code exactly, I put the piece in question into the project I'm currently working on. Both worked to my surprise. I'm running 4.1 Build 2663 on a Roku 2 3100X. I placed in a roscreen...

I was expecting it to be a problem since you weren't calling wait(0, port) in the for loop... but it ran fine for me...
YungBlood

Bringing more fun to Roku!
0 Kudos
renojim
Community Streaming Expert

Re: Reading events with 'for each' crashes Roku

"belltown" wrote:
Still, I don't see why the Roku would crash in the first case if the for-each and while loops are supposed to be equivalent ways to read event messages. Does this seem like a firmware bug or a "feature"?

They (Roku) have said on more than one occasion that anything that crashes the box is considered a firmware bug. It probably worked before the Home key started spewing invalid messages (several firmware versions ago). This would probably be a pretty low priority for them to fix. The normal way to do a message loop is to use the while with the wait function; just make sure to check for invalid messages after the wait.

"belltown" wrote:
Out of curiousity, renojim, did the first example also crash your Roku?

Yep. Crashes v3 and v4.

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.