Sign in to follow this  
Mathius20

C++ & Compiler question

Recommended Posts

Hey, I had a few questions about classes and memory. First of all, im very very new to oop, and im getting better, but im having problems with something. I am working on an animation system for my tile engine, and I am having problems with memory. I always end up creating a global pointer in the file im going to use the animation in, then in my init function, doing a new Blah(), but then since I have other classes that need to use this same copy, i end up having to use externs all over the place. So my question is what is the best way to have one copy of a pointer that you can use in multiple files(and obviously have it initialized). My other question is is there a way in 2005 .net to find the line where an error is occuring if you have the hex for it?(0x00397000 for example, Im having an access error) I dont know how to debug at all, and id like to learn how to watch variables etc. Thanks for any help. Edit:Found the line thats crashing. Its when I call m_pAnimationSprite->Begin(D3DXSPRITE_ALPHABLEND); within a function, but the thing is I have the same code that gets called for my tiles and it works fine, so I dunno what could be wrong, and since I cant debug it makes it really hard to find out whats wrong^_^ [Edited by - Mathius20 on May 22, 2006 3:36:01 AM]

Share this post


Link to post
Share on other sites
Not sure if this is what you need for your first question, but you should look at the Singleton pattern. http://en.wikipedia.org/wiki/Singleton_pattern
Otherwise you should try to redesign your code so you avoid having global pointers.

Share this post


Link to post
Share on other sites
In your debug window look at the "watch window" and check the value of "m_pAnimationSprite". It could be because that is a garbage pointer. Make sure the sprite interface is initialized and valid.

About your other problem (which looks like it maybe directly related to you access violation problem). if you have other classes that need to use the sprite interface then why not just pass them a copy during construction?



class Object
{
public:
Object( ID3DXSprite* s_ )
: m_sprite(s_)
{
assert(m_sprite);
m_sprite->AddRef();
}

~Object()
{
m_sprite->Release();
}
private:
ID3DXSprite* m_sprite;
};




A quick explanation of the AddRef/Release combination would be:

"COM uses reference counting to keep track of object lifetimes. When you get a COM object, you need to AddRef the object, so that it doesn't get destroyed while you are still using it. When you are finished with the object, you call release. This is a very simple explanation, but the important thing to remember is that every AddRef should be matched with a Release, otherwise the COM object will never be freed."

Took it from here
Or another way is that you can create a singleton out of you sprite interface:


// sprite.h
class Sprite
{
public:
static ID3DXSprite* Ptr() { assert(m_sprite); return m_sprite; }
static void Init( IDirect3DDevice9* dev ) { // init the m_sprite }
private:
static ID3DXSprite* m_sprite;
};

// some_file.h
#include "sprite.h"

void a_function()
{
Sprite::Ptr()->Begin(...);
}



Search for "singleton" on gamedev.net and you'll find out what it is and different techniques of creating it really quickly.

Share this post


Link to post
Share on other sites
Hi Mathius20,

About your needing a pointer that can be initialized and shared by all:

Try encapsulating the pointer as a static pointer inside a class. Have a member function of the class to "initialize" that pointer each time a new object is created.


Example Code START**************************************

class ptrEncapsulator
{
public:
static const animationEngine* animationEnginePtr;
static initPTR(const animationEngine* ptr) {animationEnginePtr = ptr;}
};

Example Code END****************************************


Take note that of following:
1) Pointer is made static so that globally there is only one copy of this pointer that all can share.
2) Pointer is encapsulated inside a class now so not everybody can just edit it but can only do so through the encapsulating class method "initPTR". Hence it is a safer pointer now.
3) Only a static member function can access a static member variable in a class hence the "initPTR" function also has to be static.
4) I have not considered any multi-threading issues here by making the pointer static. If anybody knows of any possible issues, please share around.

So actually there is no need for singletons here. Too complicated. IMHO.

Hope this solves your problem.



Joshua



EDIT: Yeah. Forgot that I made the static pointer also a const pointer, which means once it is initialized it will stay the same throughout whole program. This would be a good thing if you need a single constant pointer throughout. If you need to change the pointer then you should remove the "const" keyword from the static pointer.

Share this post


Link to post
Share on other sites
Thank you so much everyone for your responses. That is so incredibly simple. I remember seeing the add/ref etc before, and kenw what it did, but it never occured to me that that could fix my problem. Same with the copy constructor. I learned how to use abstract classes and polymorphism before learning how to use a copy constructor hehe. Thanks alot everyone. Off to fix my dumb global pointers^_^

Edit: Sorry forgot one thing. I tried using the watch window, and it says that the the symbol "blah" is not found, even on variables I know for a fact exist. Any ideas?

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