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

Author Topic: [SOLVED]sf::Sprite::SetImage() crash  (Read 2546 times)

0 Members and 1 Guest are viewing this topic.

Masadow

  • Newbie
  • *
  • Posts: 21
    • View Profile
[SOLVED]sf::Sprite::SetImage() crash
« on: October 30, 2011, 11:05:35 pm »
Hi,

I searched for hours but i can't find why this code crash

Code: [Select]
ge::Menu::Menu(std::string bg_path)
{
m_bg = new sf::Sprite;

std::cout << "receive addr " << ge::ResourceManager::getInstance()->getImage(bg_path) << std::endl;
sf::Image &test = *ge::ResourceManager::getInstance()->getImage(bg_path);
m_bg->SetImage(test);
}


Code: [Select]
sf::Image* ge::ResourceManager::getImage(std::string path)
{
Item *it;

if ((it = search(IMAGE, path)))
{
return (sf::Image*)(it->ptr);
}
sf::Image img;
if (img.LoadFromFile(path))
{
sf::Image *i = (sf::Image*)insert(&img, IMAGE, path)->ptr;
std::cout << "Create image at addr " << i << std::endl;
return i;
}
return NULL;
}


And the console output

Quote

Create image at addr 0x28fb40
receive addr 0x28fb40
cast addr 0x28fb40


The crash seems to be a Segmentation Fault on sf::Sprite::SetImage() call.
Someone can give me a tip ? Or more code is needed ?

Thanks in advance

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
[SOLVED]sf::Sprite::SetImage() crash
« Reply #1 on: October 30, 2011, 11:21:26 pm »
Is this SFML 2?

You're using "new" to instantiate a new Sprite. Yuk!! Not good. I would sort that out first of all.
SFML 2.1

Masadow

  • Newbie
  • *
  • Posts: 21
    • View Profile
[SOLVED]sf::Sprite::SetImage() crash
« Reply #2 on: October 30, 2011, 11:54:37 pm »
Yeah, sf::Sprite pointer was a test, forgot to change :p

I use SFML 1.6

Maybe i should give you the Item struct returned by insert().

Code: [Select]
struct Item
{
void *ptr;
std::string name;
enum Type type;
struct Item *next;
};

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED]sf::Sprite::SetImage() crash
« Reply #3 on: October 31, 2011, 07:42:06 am »
Quote
Code: [Select]
sf::Image *i = (sf::Image*)insert(&img, IMAGE, path)->ptr;

You store the address of a variable that will no longer exist after the function returns. So when you try to use it later, it crashes.
Laurent Gomila - SFML developer

Masadow

  • Newbie
  • *
  • Posts: 21
    • View Profile
[SOLVED]sf::Sprite::SetImage() crash
« Reply #4 on: October 31, 2011, 10:55:23 am »
uh... how can i be so stupid :p


Merci Laurent x)