XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

XNA Quits After Assigning Texture to Custum Class

Last post 9/14/2009 12:26 AM by Jim Perry. 9 replies.
  • 9/12/2009 3:12 PM

    XNA Quits After Assigning Texture to Custum Class

    *SOLVED - See below*

    I wrote a wrapper class for an animated sprite which takes a texture from the Game class.  The texture loads fine and passes into the constructor fine but crashes on the line commented:

    public AnimatedSprite(Texture2D text, Rectangle bounds, Rectangle sourceBounds, int frameCount, int spriteOffset)
    {
         this.texture = text; //Crash occurs here.
         this.frameCount = frameCount;
         this.spriteOffset = spriteOffset;
         this.sourceBounds = sourceBounds;
         this.isLooped = true;
         this.bounds = bounds;
    }

    However there is no error given; it just exits with code 0.  'texture' is a public property of AnimatedSprite.  Any help is certainly appreciated because I'm completely lost.

    *SOLUTION*

    The setter was self assigning which leads to a recursive self-assigning function which results in a stack overflow.  Unfortunately the stack overflow error is 0 for some reason.  I made the following changes:

    ORIGINAL CODE
    ----------------------------
    public Texture2D texture
            {
                get
                {
                    return texture;
                }
                set
                {
                    texture = value;
                }
            }
    ----------------------------

    NEW CODE
    ----------------------------
    protected Texture2D texture;
    public Texture2D Texture
            {
                get
                {
                    return texture;
                }
                set
                {
                    texture = value;
                }
            }
    ----------------------------

    I also repeated this for every property in the class.
  • 9/12/2009 4:52 PM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    Look at the implementation of texture.set.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 9/12/2009 5:02 PM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    public Texture2D texture
    {
      get
      {
        return texture;
      }
      set
      {
        texture = value;
      }
    }

    Very simple implementation.
  • 9/12/2009 8:32 PM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    Nuvious:
    However there is no error given; it just exits with code 0. 

    When you debug this happens? Also, try putting some exception handling in there.
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 9/12/2009 11:58 PM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    Adding the try-catch resulted in the same behavior.  Also Exit 0 is the code for exiting successfully, so it makes sense that no exception was caught.
  • 9/13/2009 12:34 AM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    So putting a breakpoint on that line and stepping through it just completely crashes? Is the texture you're passing still valid on that line before you step over it?
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 9/13/2009 12:11 PM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    Nuvious:
    public Texture2D texture
    {
      get
      {
        return texture;
      }
      set
      {
        texture = value;
      }
    }


    Property returning/assigning itself causes stack overflow, right? :]
    Lost C1tY
  • 9/13/2009 11:25 PM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    Does it? 0.o

    Please elaborate w/link or explanation.

    *EDIT* Never mind.  Can't believe I didn't catch that.  It would still be nice if the error wasn't 0 on a stack overflow.
  • 9/13/2009 11:36 PM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    You're basically putting your game into an endless loop. You have a property called "texture" that sets a property called "texture" and gets a value called "texture". So anytime you try to retrieve a value or set a value, the code just continually loops.

    You're like, hey, set this object "texture" equal to something and the code is like sure, no problem, let me just check inside my setter what object I'm supposed to store that in. Oh, here it is, it's called texture, let me just set that value. Oh looks like a property, let me just check inside my setter to see what object I'm supposed to store that in. Oh here it is, it's called "texture", let me just set that value. Oh looks like a property, let me just check in side my setter to see what object I'm sup....well you get the point.

    Basically your Getter and Setter should be getting and setting an object that isn't your property's name.

    Crap...just saw you figured it out :)  And after I wrote that nice explanation of recursion on properties! Oh well, glad you found it!
  • 9/14/2009 12:26 AM In reply to

    Re: XNA Quits After Assigning Texture to Custum Class

    Nuvious:
    It would still be nice if the error wasn't 0 on a stack overflow.

    If you had actually debugged this as I'd told you to do you would have spotted it immediately.
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
Page 1 of 1 (10 items) Previous Next