Sign in to follow this  
ryan20fun

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

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 [url="http://www.catb.org/~esr/faqs/smart-questions.html"]this[/url] to improve how you ask programming questions.

Share this post


Link to post
Share on other sites
[quote name='ryan20fun' timestamp='1305460524' post='4811039']
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
[/quote]

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.

Share this post


Link to post
Share on other sites
my animated sprite class has not changed much since i posted it [url="http://www.gamedev.net/topic/602010-why-does-doing-a-simple-math-check-cause-a-lpd3dxsprite-id3dxsprite-to-break/"]Here.[/url]
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
:):):)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1305472307' post='4811095']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
A simple rule is to make [b]every class[/b] 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):
[code]
// 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
{
// ...
};
[/code]

Share this post


Link to post
Share on other sites
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):
[code]
// 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);
}

// ...
};
[/code]
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this