Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Baffled by problem and need help  (Read 5152 times)

0 Members and 1 Guest are viewing this topic.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Baffled by problem and need help
« on: March 27, 2014, 07:44:03 pm »
OK, so I have just the strangest problem and I can't figure out why it is happening.

I have this bit of code, which makes the error.

bool nextLayerBlocked = false;

        for( unsigned int i = 0; i < interactiveElements.size() && nextLayerBlocked == false; ++i )
        {
            std::cout << "Processing layer " << i <<" of gui" << std::endl;

            for( unsigned int j = 0; j < interactiveElements[i].size(); ++j )
            {
                std::cout << "Processing element " << j << " of layer " << i << std::endl;

                interactiveElements[i][j]->HandleInput( userInput );

                if( interactiveElements[i][j]->IsLayerBlocking() == true )
                {
                    std::cout << "Next Layer is blocked" << std::endl;

                    nextLayerBlocked = true;
                }

                if( interactiveElements[i][j]->IsBlocking() == true )
                {
                    std::cout << "Element is blocking" << std::endl;

                    activeElement = interactiveElements[i][j];

                    j = interactiveElements[i].size();
                    i = interactiveElements.size();
                }

                std::cout << "j = " << j << " i = " << i << std::endl;
            }
        }
 

Now, when ran in the debugger, it works exactly as expected. When not run in debugger, however, it crashes. What happens not in the debugger is that when an element blocks and it sets j and i to be equal to the sizes to exit the loop, the for loops ignore their check! The cout's report the numbers as being 1 larger than they are at the end, like the loops increase them by 1 and say, yup, these numbers are still smaller than the vector size. This in turn causes a segment fault.

What baffles me is the fact that it works fine when run through the debugger, but then fails when not. I don't understand. It's probably something small and stupid, but I can't find it.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Baffled by problem and need help
« Reply #1 on: March 27, 2014, 07:54:33 pm »
Further, commenting out the section where j and i are set to the vector size, it does not create the error. Why would the loop increment the values to be higher than the vector size and then ignore it's check when it is clearly false?

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Baffled by problem and need help
« Reply #2 on: March 27, 2014, 07:58:13 pm »
When it creates the error, it will also skip

std::cout << "Processing layer " << i <<" of gui" << std::endl;
 

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Baffled by problem and need help
« Reply #3 on: March 27, 2014, 08:02:16 pm »
Setting j and i equal to the vector size - 1 fixes the problem, but it still begs the question of why it didn't work to begin with. Near as I can tell, it should have. I am truly puzzled.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Baffled by problem and need help
« Reply #4 on: March 27, 2014, 08:12:17 pm »
Quote
Baffled by problem and need help
General discussions is the wrong forum, there's a whole section about help requests. Furthermore, this is a very bad thread title. With so many posts, you should know that ;)

Also, you can use the edit function instead of 4 posts in a row. That keeps the description more readable, readers will still be notified about the change.
« Last Edit: March 27, 2014, 08:14:19 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Baffled by problem and need help
« Reply #5 on: March 27, 2014, 09:27:39 pm »
Quote
Baffled by problem and need help
General discussions is the wrong forum, there's a whole section about help requests. Furthermore, this is a very bad thread title. With so many posts, you should know that ;)

Also, you can use the edit function instead of 4 posts in a row. That keeps the description more readable, readers will still be notified about the change.

Yeah, I lost my mind thinking on this and these rules evaded me :(

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Baffled by problem and need help
« Reply #6 on: March 28, 2014, 02:24:44 pm »
After the value of 'i' had been set to:
Code: [Select]
interactiveElements[i].size()
what do you think happens to the termination condition of the inner loop on its next iteration?
« Last Edit: March 28, 2014, 02:32:40 pm by Lee R »

 

anything