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

Author Topic: Error Access violation reading location 0x8 during drawing sprite! I HAVE NO WAY  (Read 5972 times)

0 Members and 1 Guest are viewing this topic.

vitek_31

  • Newbie
  • *
  • Posts: 1
    • View Profile
Dear users,
It,s nice to meet all here. Sometimes I meet this forum to found some asnwers to my questions. But after this trouble I have no way, please help me!
Some words about code: I studing in the University and now I need to complete my project. I'm doing game on C++ with SFML. I done my class for buttons. All ok, besides one. In my game, I need to use array of buttons. Just in my game are many buttons. There is beginning my trouble. I use dynamic memory. In start I allocale memory (dynamic array of buttons), after I sey my buttons, and after I need to draw it. But when I try to draw button I get warning about access to memory. I can't resolve this bug.
When I try to drawButton I get warning:
— Exception thrown at 0x00007FF7FFD7B766 in dab_project.exe: 0xC0000005: Access violation reading location 0x0000000000000008.
        ribs_button[0][0] = ButtonRectangle(Vector2f(171+25,21), Vector2f(100, 25), window);
        ribs_button[0][0].setButtonTexture("recourses/rib.png", Vector2i(100, 75));
        ribs_button[0][0].drawButton();
 
BUT, when I get button from array and write it to separated variable all are going without expections! See example, this code done successfully.
        ribs_button[0][0] = ButtonRectangle(Vector2f(171+25,21), Vector2f(100, 25), window);
        ribs_button[0][0].setButtonTexture("recourses/rib.png", Vector2i(100, 75));
        ButtonRectangle button1 = ribs_button[0][0];
        button1.drawButton();
 
I already watched my memory via watch in Visual Studio, all variables is right in array and ribs_button[0][0] coincide with button1. Warning caused by (*window).draw() from my button class function drawButton. I find out it after debug. I was afraid what this expection cause by error while memory allocating. But all vars coincide and writes in ribs_button correctly. Now I afraid, what my expection cause by peculiarity of SFML.
I can solve it how I do it via button1, but it does not suit me!
Code from my class:
        void ButtonRectangle::drawButton() {
                MousePos = Mouse::getPosition(*window);
                //входит ли точка в прямоугольник.
                contain = ((MousePos.x >= position.x) && (MousePos.x <= position.x + size.x) && (MousePos.y >= position.y) && (MousePos.y <= position.y + size.y));
                if (contain) {
                        if (Mouse::isButtonPressed(MouseClickButton)) {
                                if (active) {
                                        (*window).draw(ButtonSpritePressed); //рисуем нажатую кнопку
                                }
                                else if (stopTimer() > 10) {
                                        active = 1;
                                        (*window).draw(ButtonSpritePressed);
                                }
                                else {
                                        (*window).draw(ButtonSpriteSelected);
                                }
                        }
                        else {
                                (*window).draw(ButtonSpriteSelected); //рисуем активную кнопка
                                if (!Mouse::isButtonPressed(MouseClickButton))
                                        setTimer();
                                std::cout << "active" << std::endl;
                        }
                }
                else {
                        (*window).draw(ButtonSprite); //рисуем обычную кнопку
                        /*active = 0;
                        stopTimer();*/

                }
        }
 

        void Buttons::setButtonTexture(String name, Vector2i sizet) {
                TextureSize = sizet;
                if (!ButtonTexture.loadFromFile(name)) std::cout << "Текстура не найдена!" << std::endl;
                ButtonSprite.setTexture(ButtonTexture);
                ButtonSprite.setTextureRect(IntRect(0, 0, TextureSize.x, TextureSize.y));
                ButtonSprite.setPosition(position);
                ButtonSpriteSelected.setTexture(ButtonTexture);
                ButtonSpriteSelected.setTextureRect(IntRect(0, TextureSize.y, TextureSize.x, TextureSize.y));
                ButtonSpriteSelected.setPosition(position);
                ButtonSpritePressed.setTexture(ButtonTexture);
                ButtonSpritePressed.setTextureRect(IntRect(0, TextureSize.y * 2, TextureSize.x, TextureSize.y));
                ButtonSpritePressed.setPosition(position);
        }
 

Constructor:
ButtonRectangle::ButtonRectangle(Vector2f pos, Vector2f sizes, RenderWindow * wind) {
        position = pos;
        size = sizes;
        window = wind;
        MouseClickButton = Mouse::Button::Left;
}
 

Memory allocale:
        ButtonRectangle **ribs_button = (ButtonRectangle**)calloc(2 * newGame.m + 1, sizeof(ButtonRectangle*));
        for (size_t i = 0; i < m + 1; i += 2) {
                ribs_button[i] = (ButtonRectangle*)calloc(newGame.n, sizeof(ButtonRectangle));
        }
        for (size_t i = 1; i < 2 * m + 1; i += 2) {
                ribs_button[i] = (ButtonRectangle*)calloc(newGame.n + 1, sizeof(ButtonRectangle));
        }
 
I have matrix with n elements in even rows and n + 1 elements in odd rows.
Sorry for my english, I am russian boy.
— Victor

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
I always recommend students working on uni projects to talk to their prof or assistants, they are being paid to help you. ;)

Access violation usually means that you tried to access memory that doesn't exist, for example trying to access a nullptr.
This can quite easily be debugged with a debugger. Load up the program in the debugger and when it crashes inspect the variables. It's very likely that [0][0] doesn't actually exist.

        ButtonRectangle **ribs_button = (ButtonRectangle**)calloc(2 * newGame.m + 1, sizeof(ButtonRectangle*));
        for (size_t i = 0; i < m + 1; i += 2) {
                ribs_button[i] = (ButtonRectangle*)calloc(newGame.n, sizeof(ButtonRectangle));
        }
        for (size_t i = 1; i < 2 * m + 1; i += 2) {
                ribs_button[i] = (ButtonRectangle*)calloc(newGame.n + 1, sizeof(ButtonRectangle));
        }
Or this might as well be your issue. You can't use C-functions to allocate C++ classes.
C++ has the keyword new and delete to allocate memory.
But you shouldn't really manually manage memory and instead use smart pointers and/or instead of using arrays use std::vector which will do all the allocations for you.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/