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

Author Topic: Access violation reading location 0x00000004.  (Read 8527 times)

0 Members and 2 Guests are viewing this topic.

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Access violation reading location 0x00000004.
« on: December 23, 2016, 12:17:32 pm »
Exception thrown at 0x0F967B0E (sfml-graphics-d-2.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00000004.

If there is a handler for this exception, the program may be safely continued
I keep getting this error in a program i created for animation using sprites.I'm new to SFML so really any help will be appreciated.Before you all start telling me that i messed up to link the debug files,I have already triple,quadruple checked it.No i have linked to the debug files
I created a class for animation called animatrix and after scrutiny i found it which line was causing this particular error.(yeah i don't know how a line would cause a reading error in a dll)
Here's the main part:-

#include <SFML\Graphics.hpp>
#include <iostream>
#include"Animatrix.h"
int main()
{
        sf::RenderWindow window(sf::VideoMode(1360, 720), "Larger SFML", sf::Style::Default);
        sf::Texture texture;
        texture.loadFromFile("bahamut.png");
        texture.setSmooth(true);
        Animatrix first(&texture,sf::Vector2u(4,4), 2.f);
        while (window.isOpen())
        {  
                sf::Clock fps;
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                std::cout << std::endl << "I wont let u close!MUAHAHAHAHAHA!!";
                                return 0;
                                break;
                        case sf::Event::Resized:
                                std::cout << "New height" << event.size.height << " New width" << event.size.width << std::endl;
                                break;
                        case sf::Event::TextEntered:
                                printf("You have pressed =%c \n", event.text.unicode);
                                break;
                        }
                       
                }
                window.clear();
                        window.draw(first.Update(fps));
                window.display();
               
        }
       
        return 0;
}
Heres the header file of animatrix.

#pragma once
#include "SFML\Graphics.hpp"
class Animatrix
{
public:
        Animatrix(sf::Texture* texture,sf::Vector2u num,float stime);
        sf::Vector2u currentimage;
        sf::IntRect size;
        float stime, fps;
        float totaltime;
        sf::Sprite show();
        sf::Sprite Update(sf::Clock fps);
        sf::Sprite sprite;
        sf::Vector2u count;
        ~Animatrix();
};
 

Heres the definition of the functions and all of animatrix i.e animatrix.cpp:
#include "Animatrix.h"
#include<iostream>


Animatrix::Animatrix(sf::Texture* texture,sf::Vector2u num,float stime)
{
    sprite.setTexture(*texture);
        count.x = num.x;
        count.y = num.y;
        size.width= texture->getSize().x / count.x ;
        size.height = texture->getSize().y / count.y ;
        currentimage.x = 0;
        currentimage.y = 0;
        totaltime = 0.0;
        this->stime = stime;

}

sf::Sprite Animatrix::Update(sf::Clock fps)
{  
        this->fps = fps.restart().asSeconds();
        totaltime = totaltime + this->fps;
        std::cout << totaltime;
        if( totaltime >= stime )                  //ERROR IS CAUSED HERE BY THE > CONDITION.Whenever i remove
        {                                                    // >condition it works fine.It works in = condition too but whenever i
                                                              //put > sign the error crops up
                std::cout << "word";
                totaltime = totaltime - stime;
                sf::Sprite thing;
                std::cout << std::endl << "does change occur here?" << currentimage.x << std::endl;

                if (currentimage.x<count.x)
                {
                        std::cout << "WORKED!";
                        currentimage.x++;
                        thing = show();
                        return thing;

                }
                else
                {

                        std::cout << std::endl << "didit";
                        while (currentimage.x != 0)
                        {
                                std::cout << std::endl << "current<<" << currentimage.x;
                                currentimage.x--;
                        }
                        std::cout << std::endl << "HERE" << currentimage.x;
                        thing = show();
                        return thing;

                }
        }
       
}
sf::Sprite Animatrix::show()
{
        sf::Vector2u tempos;
        tempos.x = currentimage.x*size.width;
        tempos.y = currentimage.y*size.height;
        sprite.setTextureRect(sf::IntRect(tempos.x, tempos.y, size.width + tempos.x, size.height + tempos.y));
        return sprite;
}

Animatrix::~Animatrix()
{
}

(*I included iosteam for checking the errors in various segments of the code)
 PLEASE HELP I DON'T WANT TO STOP SFML HERE PLEASE!!! :'( :'(
« Last Edit: December 23, 2016, 06:13:32 pm by Laurent »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Access violation reading location 0x00000004.
« Reply #1 on: December 23, 2016, 12:26:38 pm »
Where's the code that shows the construction of the Animatrix object?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Access violation reading location 0x00000004.
« Reply #2 on: December 23, 2016, 05:53:30 pm »
added the construction of object

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Access violation reading location 0x00000004.
« Reply #3 on: December 23, 2016, 06:15:48 pm »
Use your debugger, it will tell you what's wrong. No need to put std::cout everywhere and try to guess stuff ;)
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Access violation reading location 0x00000004.
« Reply #4 on: December 23, 2016, 06:19:31 pm »
You don't seem to be returning anything from your problematic function if the condition is false.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Access violation reading location 0x00000004.
« Reply #5 on: December 24, 2016, 07:30:54 am »
it is returning a sprite "thing" both from the if part and it's else part.Where's the part which doesn't return the sprite

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10817
    • View Profile
    • development blog
    • Email
Access violation reading location 0x00000004.
« Reply #6 on: December 24, 2016, 10:31:29 am »
If totaltime >= stime is not true, you'll not be returning anything.
« Last Edit: December 24, 2016, 01:40:23 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Access violation reading location 0x00000004.
« Reply #7 on: December 24, 2016, 12:57:50 pm »
OHHHHHHHHHHHHHHHHHHHHHH THANKS A BUNCH!Stupid me :-X :-\