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

Author Topic: Delete a sprite via "delete" operator  (Read 8735 times)

0 Members and 1 Guest are viewing this topic.

JunkerKun

  • Newbie
  • *
  • Posts: 35
    • View Profile
Delete a sprite via "delete" operator
« on: December 05, 2012, 02:01:47 pm »
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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Delete a sprite via "delete" operator
« Reply #1 on: December 05, 2012, 02:12:20 pm »
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.
Laurent Gomila - SFML developer

JunkerKun

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Delete a sprite via "delete" operator
« Reply #2 on: December 05, 2012, 02:19:22 pm »
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

Quote
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.

Quote
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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Delete a sprite via "delete" operator
« Reply #3 on: December 05, 2012, 02:24:10 pm »
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
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

JunkerKun

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Delete a sprite via "delete" operator
« Reply #4 on: December 05, 2012, 02:31:56 pm »
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?
« Last Edit: December 05, 2012, 02:34:07 pm by JunkerKun »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Delete a sprite via "delete" operator
« Reply #5 on: December 05, 2012, 02:35:16 pm »
Quote
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.
Laurent Gomila - SFML developer

JunkerKun

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Delete a sprite via "delete" operator
« Reply #6 on: December 05, 2012, 02:44:58 pm »
Quote
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++.
« Last Edit: December 05, 2012, 03:31:54 pm by JunkerKun »