Public Group

# Dynamic Memory Issues (and the like)

This topic is 4551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ugh, I am so sick and tired of these stupid memory things. Seems like every class I make for my game has this :-/ Ok so I'm making a sprite class right now. I have a "sprite manager" class that has a vector full of animations. The animations have a dynamic structure of "sequences". The sprite manager is inside of my entity class. I'm getting: Object reference not set to an instance of an object. I know what that is, but I have no idea how I am getting it. Besides helping me fix the error (if you could :D) I'd like to know if you guys are just super good and don't get these anymore or if there is some way around getting these memory/dynamic allocation of objects errors. Here are some code snippets:
// put sequence in the same area to show u what it is

struct sequence
{
DWORD delay;  // how much delay this particular image in the animation has
RECT selection;  // the rectangle for selection based on what image is chosen
float width;  // width of the stand still image
float height;  // height of the stand still image
DWORD startTime;  // the time that the value started
collisionBox cbA;  // collision box for attack
collisionBox cbD;  // collision box for defense
};

// i define the sequence by saying sequence* s;
// then this is how i create it
void animation::create()
{
s = new sequence[numFrames];
}

// s is the sequence
RECT animation::getImgRect()
{
// return the RECT to the image
return (s[curFrame].selection);
}


here's the snippet from the sprite manager:
// here is how i define animation
std::vector<animation> ani;

// here's where i'm getting the error
RECT spriteManager::getR()
{
return (ani[currentAnimation].getImgRect());
}


any ideas? sorry :-/

##### Share on other sites
"Object reference not set to an instance of an object," sounds to me like you're compiling a C++/CLR project (VS2005) or with managed C++ extensions enabled. You might want to turn those off (it'll be in the project properties somewhere).

Anyway, generally this means you are accessing something you haven't allocated or initialized yet. At the line:
return (ani[currentAnimation].getImgRect());

What is currentAnimation when the error trips? You aren't doing any bounds checking so its possible that currentAnimation >= ani.size() which would cause you access out-of-bounds. Its also possible that ani is not even being filled correct, so ani.size() == 0.

##### Share on other sites
yeah i figured as much; well i'm definately getting tired of all these stupid overloading of the = operator :P do i have to work with stl when i'm overloading...

i mean can i just do

spriteManager& spriteManager::operator = (const spriteManager& other){	if(this != &other)	{		ani = other.ani;		numAnimations = other.numAnimations;		currentAnimation = other.currentAnimation;			}	return *this;}

or do i have to actually iterate through all the values in ani?

also, not really sure how to debug when my app is in full-screen; is there a way i can have the compiler run the program but in the bg or something (so i can stick with the code?)

i'm using visual studio 2003, directx, and windows API; thnx

(i checked the debugger and it brings up an exception when i close my program; it tells me the object thingy: First-chance exception at 0x004113aa in windya.exe: 0xC0000005: Access violation reading location 0x00000010.
Unhandled exception at 0x004113aa in windya.exe: 0xC0000005: Access violation reading location 0x00000010.)
unfortunately i can't see what's in "currentAnimation" although i've looked and set it to 0 (the first element) in every possible place known to man :-p

any other ideas?

EDIT:

ok i get a CXX0030: Error: expression cannot be evaluated for all my variables... in my sprite manager class :/

hmm, ill be working on it, if any1 gets any ideas let me know!!

[Edited by - -justin- on May 5, 2006 10:10:15 PM]

##### Share on other sites
got it to work, sorry to bother every1!!!

##### Share on other sites
Quote:
 i mean can i just do(code snipped)

Yea, that's legal. In fact if all your operator= does is pure memberwise copy, you may not even need it (the compiler-generated one may be sufficient).

When you have an error like that, if you run the program via the debugger (regular old F5, by default), it should allow you break when its hits the exception and you can see the line its executing and what the current state of the variables are, even when the program is closing down.

For VS2003 the Managed Extensions bit is under the C++ properties, I think. If you aren't explicitly using them, make sure they are off.

##### Share on other sites
Can you post the solution so if someone searches these forums they know what it is? Thanks. Oh and you may wish to edit the first post and add in [solved] or something to the topic. Thanks and glad you got it working!

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 13
• 94
• 11
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009630
• ### Who's Online (See full list)

There are no registered users currently online

×