why does calling a function when a key is pressd crash my game and not when it is hard coded

Started by
7 comments, last by rip-off 12 years, 11 months ago
Hi Again.
ive got this absolutely stupid problem.
ive setup a function that loads and sets up a animated sprite, and it works fine, granted you call it after the level is loaded or before(i set it so that it would load the data up when called).
but when i set up some code to call that exact same function when a key is pressed, it crashes my game :angry:.
so can someone point out something.
Thanks in Advance.

here is what the function calles : some unimportant stuff has been left out.

sprites.push_back(AnimatedSprite(D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, alpha), numberofframes, PosX, PosY, Type, (UINT)width, (UINT)height, fsx, fsy, ws.c_str()));
unsigned int i = sprites.size();
sprites[i - 1].Load2(device);

sprites is a std::vector

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

Advertisement
I suspect your AnimatedSprite class does not obey the rule of three. But that is just a guess, you do not provide enough information. Read this to improve how you ask programming questions.

Hi Again.
ive got this absolutely stupid problem.
ive setup a function that loads and sets up a animated sprite, and it works fine, granted you call it after the level is loaded or before(i set it so that it would load the data up when called).
but when i set up some code to call that exact same function when a key is pressed, it crashes my game :angry:.
so can someone point out something.
Thanks in Advance.

here is what the function calles : some unimportant stuff has been left out.

sprites.push_back(AnimatedSprite(D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, alpha), numberofframes, PosX, PosY, Type, (UINT)width, (UINT)height, fsx, fsy, ws.c_str()));
unsigned int i = sprites.size();
sprites[i - 1].Load2(device);

sprites is a std::vector


What does your constructor and copyconstructor for AnimatedSprite look like ?
std::vector::push_back will create a copy of whatever you pass to it, the original animatedsprite is on the stack and will be destroyed when its out of scope, if you are using heap allocations in the constructor and don't have a proper copy constructor a crash is quite likely.

(Its entierly possible that you got some other error though, its really hard to tell based on the tiny amount of code you posted) (It could also be a bug in your load function or caused by any code left out between push back and the load call), you're not even telling us which line is causing the crash or what error message you're getting.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
my animated sprite class has not changed much since i posted it Here.
but im just going to work on creating a bunch off scrren and just place them when needed.
but i still dont know how to fix this issue
:):):)

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

As noted by others in the linked thread, your class does not correctly handle being copied or assigned. As such, it does not meet the requirements of std::vector. Your options are to implement the correct copy constructor/assignment operator, or to use (smart) pointers in your vector.

As noted by others in the linked thread, your class does not correctly handle being copied or assigned. As such, it does not meet the requirements of std::vector. Your options are to implement the correct copy constructor/assignment operator, or to use (smart) pointers in your vector.


ok, i did not notice that at the time (im busy trying to iron out some errors that i thought could not happen, as i came from a more C# background and i did not have this type of problem, but then again i have only been 'actuall' been programeing fo about 2 years now)
but i will take action.

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

A simple rule is to make every class rule of three compliant, or to disable compiler generated copy constructor and assignment operators using the below (or boost::noncopyable if you are using boost):

// In some utility header

class noncopyable
{
protected:
noncopyable() {}
private:
// intentionally private and unimplemented!
// Do not copy instances of this class!
noncopyable(const noncopyable &);
noncopyable &operator=(const noncopyable &);
};

// Everywhere else =]

class MyFunkyClass : noncopyable
{
// ...
};
thanks, i was going to do the 'cheep' way and not make it call Release() on destruction, guess it has finally turned around and bit my on my "backside"

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

C# faces related problems. Languages like C# solve memory management using garbage collection. However, in this case, you are managing an external, non-memory resource. In C++ and in C# you need to structure your code to release these resources when you are not using them. C# gives you the IDisposable interface and the "using" keyword, C++ gives you RAII (which can be used to manage memory too).

Embrace RAII. Use some kind of smart pointer to manage these classes, or use the built in reference counting COM provides (just be sure you understand what this means):

// Utility header
void safe_release(IUnknown *object)
{
if(object)
{
object->Release();
}
}

template<typename T>
T *safe_addref(T *object)
{
if(object)
{
object->AddRef();
}
return object;
}

class AnimatedSprite
{
public:
AnimatedSprite()
{
// ...
}

AnimatedSprite(float PosX, float PosY)
{
// ...
}

AnimatedSprite(const AnimatedSprite &other)
{
sprite = safe_addref(other.sprite);
texture = safe_addref(other.texture);
}

AnimatedSprite &operator=(const AnimatedSprite &other)
{
AnimatedSprite copy = other;
this.swap(copy);
return *this;
}

void swap(AnimatedSprite &other)
{
std::swap(sprite, other.sprite);
std::swap(texture, other.texture);
}

~AnimatedSprite()
{
safe_release(texture);
safe_release(sprite);
}

// ...
};

Something like that should get your class working in a container. However, you still have to be careful with any copies you make yourself, because they will share the same sprite and texture. The copies could end up "fighting" over their shared data.

This topic is closed to new replies.

Advertisement