SFML community forums

Help => General => Topic started by: Nitetesi on November 20, 2021, 04:51:16 pm

Title: RenderTarget Pointer&Reference issue
Post by: Nitetesi on November 20, 2021, 04:51:16 pm
Too lazy to explain in detail

void Entity::render(sf::RenderTarget& target)
{
        target.draw(this->entity);
}
 

0xC0000005: Access violation reading location 0x0000000000000000.


Render function in Window.cpp
this->entity->render(*this->window);
 

Window.h
-------------------------
sf::RenderWindow* window;
Entity* entity;
Title: Re: RenderTarget Pointer&Reference issue
Post by: eXpl0it3r on November 21, 2021, 10:20:19 pm
You're probably reading or writing to a nullptr.

Too lazy to explain in detail ;)
Title: Re: RenderTarget Pointer&Reference issue
Post by: Nitetesi on November 27, 2021, 07:49:27 pm
You're probably reading or writing to a nullptr.

Too lazy to explain in detail ;)

Could you explain?
Title: Re: RenderTarget Pointer&Reference issue
Post by: Nitetesi on November 28, 2021, 03:06:01 pm
You're probably reading or writing to a nullptr.

Too lazy to explain in detail ;)
void Entity::render(sf::RenderTarget& target)
{
        this->setEntity();
        target.draw(this->entity);
}
Nothing looks undefined and now i am seeing that error on setEntity() function

void MainWindow::render()
{
        this->setVariables();
        this->window->clear(this->gray);
        this->window->draw(this->posText);
        this->entity->render(*this->window);
        this->window->display();
}
Title: Re: RenderTarget Pointer&Reference issue
Post by: kojack on November 28, 2021, 03:33:22 pm
"Access violation reading location 0x0000000000000000" means the program is trying to read from location 0 in memory, which isn't allowed. This is typically caused by using a pointer which is null.
If you run the program in a debugger, it should stop on the actual location of the null dereference. The code you provided might not actually be where things are failing (if it is, then probably either window or entity are null).
Title: Re: RenderTarget Pointer&Reference issue
Post by: Nitetesi on November 28, 2021, 06:15:16 pm
"Access violation reading location 0x0000000000000000" means the program is trying to read from location 0 in memory, which isn't allowed. This is typically caused by using a pointer which is null.
If you run the program in a debugger, it should stop on the actual location of the null dereference. The code you provided might not actually be where things are failing (if it is, then probably either window or entity are null).

The address is now static
Access violation reading location 0x0000000000000108.
And i am sure that the problem is Entity
(https://i.imgur.com/dS1Dggs.png)

Title: Re: RenderTarget Pointer&Reference issue
Post by: kojack on November 28, 2021, 08:10:38 pm
Yep, the Entity pointer there (the "this" value) is null.
Maybe the code that set it up wasn't called, or something failed.

Values of pointers close to 0 (like 0x0000000000000108) are still from a null pointer. It's using the null as the base address of an object that doesn't exist, then offsetting from that to find member variables (resulting in the error at a near 0 value).
Title: Re: RenderTarget Pointer&Reference issue
Post by: Nitetesi on November 29, 2021, 09:10:05 am
Yep, the Entity pointer there (the "this" value) is null.
Maybe the code that set it up wasn't called, or something failed.

Values of pointers close to 0 (like 0x0000000000000108) are still from a null pointer. It's using the null as the base address of an object that doesn't exist, then offsetting from that to find member variables (resulting in the error at a near 0 value).

Yeah i fixed that by making a function that initializes Entity

void MainWindow::initializeEntity(Entity& entity)
{
        this->entity = &entity;
}