0

In my WP7 app, InvalidOperationException is being thrown in the device (while it works fine in emulator). From the stacktrace provided by Microsoft, it seems the source might be following code.

Image start_rec = (Image)FindName(start_name);
// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = canvas1.TransformToVisual(start_rec);
pt = generalTransform1.Transform(new Point(0, 0));
double start_y = pt.Y * (-1);
double start_x = pt.X * (-1);
Image destination_rec = (Image)FindName("dot" + destination_name);
// Return the general transform for the specified visual object.
generalTransform1 = canvas1.TransformToVisual(destination_rec);

// Retrieve the point value relative to the child.
pt = generalTransform1.Transform(new Point(0, 0));
double destination_y = pt.Y * (-1);
double destination_x = pt.X * (-1);

What I am trying to do in above code: There are images in the canvas1. I am trying to get the location of the images wrt the canvas.

My question is why I am getting the Exception ?

Edit : Stacktrace is

Frame    Image             Function                                            Offset    
0        coredll.dll       xxx_RaiseException                                  19        
1        mscoree3_7.dll                                                        436172    
2        mscoree3_7.dll                                                        383681    
3        mscoree3_7.dll                                                        540620    
4                          TransitionStub                                      0         
5                          Microsoft.Xna.Framework.Audio.SoundEffect.Play      200       
6                          Microsoft.Xna.Framework.Audio.SoundEffect.Play      68        
7                          BoxIt.MainPage.correct_line                         1780      
8                          BoxIt.MainPage.evaluate                             360       
9                          BoxIt.MainPage.canvas1_ManipulationCompleted        196       
10                         MS.Internal.CoreInvokeHandler.InvokeEventHandler    2752      
11                         MS.Internal.JoltHelper.FireEvent                    1324      
12       mscoree3_7.dll                                                        428848    
13       mscoree3_7.dll                                                        430212    
14       mscoree3_7.dll                                                        610999    
15       mscoree3_7.dll                                                        374145    
16                                                                             0         
17       agcore.dll        CCoreServices::CLR_FireEvent                        385       
18       npctrl.dll        CControlBase::ScriptCallback                        435       
19       npctrl.dll        CXcpDispatcher::OnScriptCallback                    547"

Edit 2: Code inside function correct_line()

void correct_line(int a_h, int a_v)
    {
        Point pt;
        int destination_num;
        string destination_name;

        string[] words = start_name.Split('t');
        //textBox1.Text = words[1];

        int.TryParse(words[1], out destination_num);
        int start_num = destination_num;
        destination_num = destination_num + a_h + a_v * 4;
       // textBox1.Text = start_num + "  " + destination_num;
        Boolean u = true;
        if (list.Count > 0)
        {
            u=!(list.Contains(start_num + destination_num));
        }
        if (u)
        {
            if (start_num > 0 & destination_num > 0)
            {
                int po = Math.Abs(start_num-destination_num);
                if(po==1 | po==4)
                {
                destination_name = (destination_num).ToString();
                Image start_rec = (Image)FindName(start_name);
                // Return the general transform for the specified visual object.
                GeneralTransform generalTransform1 = canvas1.TransformToVisual(start_rec);
                pt = generalTransform1.Transform(new Point(0, 0));
                double start_y = pt.Y * (-1);
                double start_x = pt.X * (-1);
                Image destination_rec = (Image)FindName("dot" + destination_name);
                // Return the general transform for the specified visual object.
                generalTransform1 = canvas1.TransformToVisual(destination_rec);

                // Retrieve the point value relative to the child.
                pt = generalTransform1.Transform(new Point(0, 0));
                double destination_y = pt.Y * (-1);
                double destination_x = pt.X * (-1);

                //textBox1.Text = " " + start_num + " " + a_h + " " + destination_num + " " + a_v ;
                Line line = new Line() { X1 = start_x + dotwidth / 2, Y1 = start_y + dotwidth / 2, X2 = destination_x + dotwidth / 2, Y2 = destination_y + dotwidth / 2 };
                if (msg == "2" & move == 2)
                {
                    line.Stroke = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    line.Stroke = new SolidColorBrush(Colors.White);
                }
                line.StrokeThickness = 15;
                line.SetValue(Canvas.ZIndexProperty, 25);
                line.Tag = "correct_line";
                this.canvas1.Children.Add(line);
                if (gsplay == 1)
                {
                    //stroke.Play();
                    strokemusic.Play();
                }
                //
                update_matrix(start_num, destination_num);
                list.Add(start_num + destination_num);


                //
                h = 0;
                v = 0;
                //move = checkfor_box(start_num, destination_num, a_h, a_v);
                move = checkfor_box3();
                find_move();

            }
            }
        }
    }

Soundeffect is initiated in OnNavigatedTo function

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);


        if (settings.Contains("gs"))
        {
            string hx = (string)settings["gs"];

            if (hx == "1")
            {
                gsplay = 1;
                var laserStream1 =  Application.GetResourceStream(new Uri("boxdone.wav", UriKind.Relative));

                boxdonemusic = SoundEffect.FromStream(laserStream1.Stream);



                var laserStream2 =  Application.GetResourceStream(new Uri("stroke.wav", UriKind.Relative));

                strokemusic = SoundEffect.FromStream(laserStream2.Stream);

            }
            else
            {
                gsplay = 0;
            }
        }



    }
Ashni Goyal
  • 819
  • 3
  • 10
  • 20
  • 2
    "From the stacktrace provided by Microsoft"? Are you referring to the stacktraces in the devcenter? Are you able to reproduce the crash on your own device? Also, it would help if you could provide the full stacktrace. – Kevin Gosse Oct 25 '12 at 15:11
  • @KooKiz Yes, devcenter provides the stacktrace for the crashes. I do not have a device, so i cannot check it. I have added the stacktrace here, kindly help. – Ashni Goyal Oct 25 '12 at 16:21
  • I am new to stacktraces, could you tell me at what frame the `exception` is being generated ? – Ashni Goyal Oct 25 '12 at 16:25
  • First of all, you just have to understand that some of the stacktraces on devcenter are generated by devices in unstable states. What it means is that you won't be able to reproduce all the crashes, because some of them don't occur in normal conditions. Now, back to your stacktrace. The first frame containing your code is `canvas1_ManipulationCompleted`. Then it calls `evaluate`, which calls `correct_line`. This method tries to play a sound, and that's when the crash occurs. The code you've put in the question isn't relevant at all, the exception is somewhere else. – Kevin Gosse Oct 25 '12 at 18:48
  • I have added the required codes – Ashni Goyal Oct 25 '12 at 19:10
  • the error report from dev center mentions that the `exception` is `invalidoperationexception` and this type is thrown by `Transform` function only , other function in the code throw other type of exceptions, that is why I was thinking that problem might be with `Transform` portion of code. Btw , what ur analysis says ? – Ashni Goyal Oct 25 '12 at 19:15
  • Just answered with 'my analysis'. But you should really test the application on a real device, just in case. – Kevin Gosse Oct 25 '12 at 19:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18599/discussion-between-kookiz-and-ashni-goyal) – Kevin Gosse Oct 25 '12 at 19:25

1 Answers1

1

The exception is thrown at strokemusic.Play(); line of your correct_line method, that much is certain. Now the question is to know why.

After digging a bit, it seems like the two main causes for having an InvalidOperationException when playing a sound are:

  • The sound isn't stored in a supported format. It is unlikely as it would probably crash in the emulator as well
  • The phone is connected to Zune, in which case the application isn't allowed to play a sound. It would also explain why the error occurs only on real device (the emulator isn't connected to Zune).

How to fix the problem? Simpy surround your line with a try/catch clause:

try
{
    strokemusic.Play();
}
catch (Exception) { }
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94