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

Author Topic: Multiple function crashes - need help!  (Read 1255 times)

0 Members and 1 Guest are viewing this topic.

Snail

  • Newbie
  • *
  • Posts: 6
    • View Profile
Multiple function crashes - need help!
« on: March 30, 2013, 06:29:58 am »
Hi,

I am creating a game and have gotten to the point of drawing stuff on the screen now. However, I am getting crashes across multiple functions including RenderWindow::clear and RenderWindow::draw.

I have built SFML and my program in debug mode to trace where things are going wrong. (It's getting an access violation error trying to read memory at 0x00000008) Both functions seem to crash when calling a function called activate, seen here:

Code: [Select]
void RenderTarget::draw(const Vertex* vertices, unsigned int vertexCount,
                        PrimitiveType type, const RenderStates& states)
{
    // Nothing to draw?
    if (!vertices || (vertexCount == 0))
        return;

    if (activate(true)) // <-------- crashes here
    {

Code: [Select]
////////////////////////////////////////////////////////////
void RenderTarget::clear(const Color& color)
{
    if (activate(true)) // <----------- crashes here
    {

Furthermore, I have traced that function down to a function called GlContext::setActive as seen here:

Code: [Select]
bool GlContext::setActive(bool active)
{
    if (active)
    {
        if (this != currentContext)
        {
            // Activate the context
            if (makeCurrent())
            {
                // Set it as the new current context for this thread
                currentContext = this;
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            // This context is already the active one on this thread, don't do anything
            return true;
        }
    }
    else
    {
        if (this == currentContext)
        {
            // To deactivate the context, we actually activate another one so that we make
            // sure that there is always an active context for subsequent graphics operations
            return getInternalContext()->setActive(true);
        }
        else
        {
            // This context is not the active one on this thread, don't do anything
            return true;
        }
    }
}

I'm assuming one of the checks here is wrong or something like that. Some more info:

Compiler: Visual C++ 2012
Version: Compiled from the git repository. (3/30/2013)
Graphics card: AMD Radeon HD 6850

The source code I am using to reproduce the error: (Uncomment the clear/draw calls to test each one, they both crash)

Code: [Select]
// untitled.cpp
// Written by Snail

#include <iostream>

#include <sfml/graphics.hpp>

#include <constants.hpp>
#include <untitled.hpp>

int main(int number_of_arguments, char **arguments)
{
sf::RenderWindow window(sf::VideoMode(800, 600), PROGRAM_NAME);

window.setVerticalSyncEnabled(true);

sf::Clock elapsed_time;
sf::Event event;

sf::Sprite player;
sf::Texture texture;

if(!texture.loadFromFile("sprite.png"))
std::cout << "Could not load sprite.png" << std::endl;

player.setTexture(texture);
player.setPosition(100, 100);

sf::Time fps;
sf::Time one_second = sf::seconds(1.0);
sf::Time zero = sf::seconds(0.0);

while(window.isOpen())
{
if(elapsed_time.getElapsedTime() >= one_second)
{
std::cout << fps.asSeconds() << std::endl;

elapsed_time.restart();
fps = zero;
}
else
fps += elapsed_time.getElapsedTime();

while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();

break;

case sf::Event::KeyPressed:
switch(event.key.code)
{
case sf::Keyboard::Escape:
window.close();

break;

default:
break;
}

break;

default:
break;
}
}

window.clear(sf::Color(255, 255, 255));
//window.draw(player);
window.display();
}

return 0;
}

If anyone has any insight into this, please let me know. Thanks!
« Last Edit: March 30, 2013, 06:35:15 am by Snail »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multiple function crashes - need help!
« Reply #1 on: March 30, 2013, 11:40:40 am »
1. Make sure the correct libraries (release/debug, static/dynamic) are used, and no configurations are mixed. Also don't mix different compiler versions.

2. If the error persists, come up with a complete and minimal example to reproduce your problem.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Snail

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Multiple function crashes - need help!
« Reply #2 on: April 01, 2013, 01:41:32 am »
I have solved the problem.

I usually compile my projects with a structure alignment of 1 byte but the SFML libraries were being compiled with the default alignment. Recompiling the SFML libraries with the structure alignment that I use has fixed my issue.