🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Access violation reading location 0xDDDDDDDD.

Started by
4 comments, last by Thinias 4 years, 4 months ago

So, I'm trying to create a sprite class for my game however I can't seem to get it to work.

When trying to render my texture using SDL_RenderCopy() it causes a exception and my game crashes with the error :


Access violation reading location 0xDDDDDDDD. 

When checking SDL_GetError() right before I try to render the texture, there are no errors.

So far, I know that the error means I'm trying to use a "bad" pointer, however I can't seem to find any bad pointers in my code.

This is what my sprite class looks like :

Sprite.h


#pragma once#include "Vector2.h"
#include "Window.h"
#include "Errors.h"
#include <iostream>
#include <SDL\SDL.h>
#include <string>


class Sprite
{
public:
    Sprite(std::string id, std::string filePath, Vector2 position, Vector2 size);


    ~Sprite();


    void Draw();


private:
    SDL_Texture* texture;
    SDL_Surface* surface;


    int x;
    int y;


};

Sprite.cpp


#include "Sprite.h"





Sprite::Sprite(std::string id, std::string filePath, Vector2 position, Vector2 size)
{
    surface = SDL_LoadBMP(filePath.c_str());
    if (surface == nullptr)
    {
        Errors::Error("Failed to load image!", __FILE__, __LINE__);
        return;
    }


    x = position.x;
    y = position.y;


    surface->w = size.x;
    surface->h = size.y;


    texture = SDL_CreateTextureFromSurface(Window::GetRenderer(), surface);
    //SDL_FreeSurface(surface);
    //surface = nullptr;
}




Sprite::~Sprite()
{
    //SDL_DestroyTexture(texture);
}


void Sprite::Draw()
{
    SDL_Rect rect;
    rect.x = x;
    rect.y = y;


    if (SDL_RenderCopy(Window::GetRenderer(), texture, NULL, &rect) < 0)
    {
        std::cout << SDL_GetError() << std::endl;
        return;
    }


}

This is what I've tried so far :

  • Checking the texture variable value, which is 0xdddddddd, so I'm guessing this is the locations it fails to read.
  • Checking my surface, since texture gets it's image from there, it has the same memory address : 0xdddddddd. However, the w/h, flags etc I can't seem to read because : <Unable to read memory>. So this is probably where the error starts. But it doesn't make sense to me, if this is where the error is created, shouldn't SDL_CreateTextureFromSuface() generate a SDL_GetError() since it's trying to use a bad pointer?
  • Checking other values such as x and y, which seem to be off entirely, I set them to 10 however when checking them they are both -572662307.

Other notes :

The if statement where I load the bitmap image doesn't seem to run, so it does load the image properly : (code is taken from Sprite.cpp, check above.)


surface = SDL_LoadBMP(filePath.c_str());
if (surface == nullptr)
{ <- This and down never runs.
    Errors::Error("Failed to load image!", __FILE__, __LINE__);
    return;
}
Advertisement

How are you creating and using the Sprite class?

The bitpattern DDDDDDDD in all variables means the whole Sprite object was deallocated before you tried to use it. (-572662307 in decimal is also 0xDDDDDDDD in hex)

So it seems you are keeping a pointer to a Sprite object after the object went out of scope.

(This is only true for debug builds. Debug memory allocators commonly write bitpatterns like this when deallocating objects to make errors like this crash faster and be easier to find)

How are you creating and using the Sprite class?

The bitpattern DDDDDDDD in all variables means the whole Sprite object was deallocated before you tried to use it. (-572662307 in decimal is also 0xDDDDDDDD in hex)

So it seems you are keeping a pointer to a Sprite object after the object went out of scope.

(This is only true for debug builds. Debug memory allocators commonly write bitpatterns like this when deallocating objects to make errors like this crash faster and be easier to find)

Aha, thank you! Solved my problem.

I did this in my SpriteManager class :


void SpriteManager::CreateSprite(std::string id, std::string filePath, Vector2 position, Vector2 size)
{
    Sprite* sprite = new Sprite(id, filePath, position, size);
    spriteList.insert(std::make_pair(id, sprite));
    delete sprite;
}
Which caused the error since I deleted it right after, doing this instead now and its working :

spriteList.insert(std::make_pair(id, new Sprite(id, filePath, position, size)));

Well, that was a lot simpler than I expected. Thanks again :)

Remember you are now responsible for deleting the Sprite when you are either removing it from the list or the list is destroyed, or you'll have memory leaks.

Modern smart pointers can automate this sort of thing for you.

Hello

I am struggling with a similar error.

If i try and delete :

Then my object does not render and a new breakpoint appears (the error doesn't invoke in the line above but randomly on run time).

Any help would be much appreciated !

0xcdcdcdcd is another special value used by many memory managers to denote that memory has not been initialized. A value of 0xcdcdcdcdcdcd0101 suggests that either your Jewel constructor has a serious flaw, or that you have a memory stomp occurring somewhere else in your application. Given how close together your constructor and the getTargetRect() invocation are, it is more likely the former. Take a close look at your Jewel constructor, and ensure that you actually initialize all of the encapsulated objects the way you think you should.

This topic is closed to new replies.

Advertisement