-
So, I've made a class for player object and something strange is going on.
Constructor:
Object::Object(sf::String path) {
image = new sf::Image;
image->loadFromFile(path);
tex = new sf::Texture;
tex->loadFromImage(*image);
spriteWidth=tex->getSize().y;
spriteHeight=tex->getSize().y;
sprite = new sf::Sprite;
sprite->setTexture(*tex);
Init();
SetOrigin(xo,yo);
SetBBox(0,0,spriteWidth,spriteHeight);
Object::SetFrame(animFrame);
renderStates.blendMode=sf::BlendNone;
delete image;
};
Destructor:
Object::~Object() {
delete sprite;
delete tex;
};
Piece of Main.cpp
window.clear();
window.draw(BG);
window.draw(rect);
window.draw(*Player.sprite);
window.draw(sprite);
window.draw(text);
window.display();
Now the problem is when I run it as it is it gives me "Acces violation" error. Can't find a sprite, I guess, 'cause it crashes on window.draw(*Player.sprite). But if I comment the whole destructor, it runs just fine. Is there anything I am missing?
Also, how does resource freeing works in SFML? I mean, resources should be removed somehow to not take memory away, right?
-
You probably copy your object somewhere, and since your copy constructor is probably not implemented correctly, you end up with a valid object holding a deleted sprite.
Don't use dynamic memory allocations: your code will be simpler and all your problems will go away.
PS: there's a Texture::loadFromFile function.
-
Thanks for the quick answer.
You probably copy your object somewhere, and since your copy constructor is probably not implemented correctly, you end up with a valid object holding a deleted sprite.
Nope, object was created only once in code. Which is why I'm wondering why it works with destructor commented O_o
Don't use dynamic memory allocations: your code will be simpler and all your problems will go away.
Well, the main reason I'm using it is because I don't know any other way to manage resources. You know, to release sprites and textures when I don't need them.
PS: there's a Texture::loadFromFile function.
I used loadFromImage because I will need to do some manipulations with pixels later, so I had to create an Image first.
-
Well, the main reason I'm using it is because I don't know any other way to manage resources. You know, to release sprites and textures when I don't need them.
Generally, take a look at the RAII idiom. It is a very simple principle, that solves a lot of problems and makes your code easier to understand.
There is no reason why you should release resources explicitly here. They are automatically released if the holder object (sf::Texture instance) is destroyed, which happens at scope exit. Instead of
image = new sf::Image; // why isn't this declared locally anyway?
... // use image
delete image;
you just write
sf::Image image;
... // use image
-
Wait, so, if I have a player object and write
sf::Sprite sprite;
in constructor and then use this object in Main.cpp it will work all the way, but when I destroy this object, sprite instance (and anything else, like Image, Music etc) deletes by itself?
But what happenes if I, for example, put instances in array and then just erase them from it?
-
Nope, object was created only once in code.
If it's created like this, there's already a copy:
Object object = Object("...");
There may also be hidden copies if you're passing or returning the object to functions by copy.
-
Nope, object was created only once in code.
If it's created like this, there's already a copy:
Object object = Object("...");
There may also be hidden copies if you're passing or returning the object to functions by copy.
//Player
Object Player("Run.png");
Player.SetOrigin(26,26);
Player.SetPosition(100,100);
Player.SetBBox(15,10,37,Player.spriteHeight-10);
This is how I create a player object. And I think I don't pass object by copy. At least I don't remember doing that...
Update:
Hm... found out why I got an error:
CollisionPoint(viewXview+Mouse.getPosition(window).x,viewYview+Mouse.getPosition(window).y,Player)
Commented it and it works fine. But how do I pass object to a function in that case? O_o
Update 2:
Gah, nevermind, I just forgot too much about c++ in common. Gotta get my textbook and learn some C++.