• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
swilkewitz

Odd memory problems with classes

45 posts in this topic

Last week I finally got around to implementing boost's shared pointers to store my actor objects in a vector. It was working perfectly until I tried adding more functionality to the base actor class. Basically, when in release mode, the objects don't draw if I add too many variables to the class. I bet that both the drawing and the memory limit are effects of a bigger problem.

I chose to turn "GLfloat close" into a magic number in order to temporarily bypass the problem, but now that I have more time I'd like to figure out the real problem. I could have done the same with a different variable, but "close" was the easiest to get rid of. Also, the "memory limit" does not apply to variables of my own classes, like "Quaternion orientation".

My guess is that the real problem has something to do with shared pointers...

[code]class Actor
{

public:
//State
Point position;
Quaternion orientation;

//Speed
Vector moveSpeed;
Vector turnSpeed;

protected:
//Model
GLfloat scale;
Model model;
GLuint UVTexture;

//Speeds
GLfloat moveAccel;
GLfloat turnAccel;
GLfloat moveCap;
GLfloat turnCap;

//Fade
static const long fadeBegin = 70;
static const long fadeEnd = 90;
static const int desiredOpacity = 5;
GLfloat opacity;
//GLfloat close;
//uncommenting this or adding another variable would cause the actors to stop displaying
//If I remember correctly, even though the objects don't display, their members are at least non-null
}[/code]
0

Share this post


Link to post
Share on other sites
This is likely a poblem of writing to the wrong memory address in another part of your program. Check out other areas of your program where you use pointers to write stuff to.
0

Share this post


Link to post
Share on other sites
Agreed - it sounds like you're smashing memory someplace. The primary suspects are anywhere you use raw pointers; shared_ptr is [i]not[/i] likely to be the culprit.
0

Share this post


Link to post
Share on other sites
Oh, thanks! Hmm...

". . . areas of your program where you use pointers to write stuff to."
Where I am writing [i]to[/i] a pointer? The wording of that sentence is a little confusing.

How would I accidentally "smash" memory? Could you give me an example of what to look for? My experience with memory management is minimal. In the meantime, I will look for raw pointers. Do they have to be connected with the actor class in some way, or can they be anywhere?

Thanks!
Scott Wilkewitz
1

Share this post


Link to post
Share on other sites
[quote name='swilkewitz' timestamp='1309399634' post='4829369']How would I accidentally "smash" memory? Could you give me an example of what to look for?[/quote]Usually the culprits are bad loops (looping too many times), or invalid casts (casting a pointer to the wrong type).[code]class Base { int a; };
class Derived : public Base { int b; }
...
Derived* derived = new Derived();
Base base = *derived;//slicing
Derived* pObject = (Derived*)&base;//invalid cast, 'base' is not a 'Derived'
pObject->b = 42;//out of bounds memory write -- pObject points to a 'Base', not a 'Derived'[/code]

[code]Object array[42];
for( int i=0; i<52; ++i )
array[i].foo = bar;//out of bounds memory write on the last 10 iterations[/code]
0

Share this post


Link to post
Share on other sites
While sudden crashes when making such minor changes usually are a result of trashed memories, as already described above, it might help to ensure you do not have a dependency problem. If one compilation unit was compiled with the old version of your header and another with the new version, bad and weird things are bound to happen. Usually it's an IDE's/makefile's job to recompile all relevant compilation units and in general they do that pretty well, but sometimes things break down (or the user made an error and a proper dependency graph cannot be constructed with the too limited information given).

In a small project, I would suggest to first make a change (like adding a newline where it does not matter), save the header and then trigger a build. All source files which include (explicitly or implicitly) the header must be recompiled, if that does not happen automatically, something with the dependencies is wrong and must be fixed first.

Admittedly, it is rather unlikely that the above issue is the cause of your problem, but I would take a minute to verify it because searching for the cause of trashed memory is generally an extremely time consuming and frequently very frustrating task.
0

Share this post


Link to post
Share on other sites
I found that taking a raw pointer of an actor out of my camera class (which is an actor) got rid of the problem. The pointer in question wasn't even being used yet. I also changed all of my methods to use references instead of raw pointers, but that didn't have any effect as far as I am aware.

[s]So did I find the final solution, or did I just find another bypass?[/s]
Edit: I just realized that it was a bypass. Taking the pointer out just got it under the "memory limit."
I'll keep looking for raw pointer use...

Thanks for all the help!
Scott Wilkewitz
0

Share this post


Link to post
Share on other sites
Ok, I got rid of all the pointers and the problem still exists.
Bitmaster, could I just rebuild my project, or would I have to add a change to all of my headers?

Thanks!
0

Share this post


Link to post
Share on other sites
[font=arial, verdana, tahoma, sans-serif][size=2]I have seen what Bitmaster is talking about. If you are using visual studio, then look at the project dependencies from the menu and make sure they are setup right per project.

If it it memory getting trashed, then right after you set the variable to the correct value - and it looks right in the debugger, then set a data breakpoint on that variables address and if the var gets overwritten then the breakpoint will fire and show you what code did it...you did mention it's a release only problem but hopefully it can be reproduced in the debug[/size][/font]
0

Share this post


Link to post
Share on other sites
[quote name='BuffaloJ' timestamp='1309454254' post='4829671']
[font="arial, verdana, tahoma, sans-serif"][size="2"]I have seen what Bitmaster is talking about. If you are using visual studio, then look at the project dependencies from the menu and make sure they are setup right per project.

If it it memory getting trashed, then right after you set the variable to the correct value - and it looks right in the debugger, then set a data breakpoint on that variables address and if the var gets overwritten then the breakpoint will fire and show you what code did it...you did mention it's a release only problem but hopefully it can be reproduced in the debug[/size][/font]
[/quote]

I only have two projects in the solution, and they are independent. Also, I don't have any raw pointers... would it still be possible to trash the memory anyway?

As for posting code, I really don't know what I would post. I can't just post everything and I don't have a clue which area the problem is coming from.
0

Share this post


Link to post
Share on other sites
You can easily trash memory without using raw pointers; there are any number of ways to do it in C++, unfortunately.

Best thing you can do at this point is start disabling/removing code ([b]not[/b] members of your classes but entire pieces of functionality) until you have a minimal program that reproduces the issue. Then you can post that.
0

Share this post


Link to post
Share on other sites
For project dependencies, you probably have a main project that builds to an exe and a library project(correct me if I'm wrong)...the main project would need to depend of the library project or changes in the library may not trigger recompiling stuff in the main. Either way, a clean and rebuild all would eliminate that as the culprit

It's possible to overwrite memory if you are writing to an array and write to an index past it's bounds. Like if it has 25 elements and you write to element 25 (zero based indexing...), memory is trashed. Putting in a data breakpoint after setting the value would tell you if something else it overwriting it...



0

Share this post


Link to post
Share on other sites
Note that array overruns and pointer mischief are not the only way to trash memory; it can be done with evil casts, references, object slicing, and so on.
0

Share this post


Link to post
Share on other sites
The projects in my solution aren't interdependent, but I cleaned and rebuilt my project anyway; that didn't do anything. I got rid of raw pointers by using references instead. I checked my loops, and the only ones that traverse any arrays are for each loops. The other loops just add objects to arraylists, and I'm pretty sure my program doesn't do any casting.

So... I don't know what object slicing is, and I use references to pass (in the World's draw actors for each loop) my camera class so that each actor can draw itself . I still don't know where to look.

Here's most of the initialization and game loop:

[code]bool Game::start()
{

//Control Target
//controlTarget = &userCamera; //Commented out raw pointer (didn't help)

//Add Asteroids
for(int i = 0; i < 75; ++i){
shared_ptr<Actor> tempA = shared_ptr<Actor>(new Asteroid());
tempA->addTexture(asteroidTexture);
tempA->addModel(asteroidModel);
tempA->setScale();
space.addActor(tempA);
}

return true;
}

bool Game::run()
{

//Get Change in Time
GLfloat dt = timer.getDT();

//Control Target
//controlTarget->control(controller, dt);

//Update World
space.act(dt);

//Draw world
space.draw(userCamera);

//Reset Actions
controller.resetActions();

return true;
}[/code]

[code]bool World::start(GLuint starTex)
{

//Add Stars
for(int i = 0; i < 1000; i++){
Body tempStar = Body(starTex);
bodies.push_back(tempStar);
}

return true;
}

bool World::act(GLfloat dt)
{

//Update Actors
for each(shared_ptr<Actor> a in actors){
a->act(dt);
}

return true;
}

bool World::draw(Actor& camera)
{

//Draw Bodies
for each(Body b in bodies){
//Load Perspective
glLoadIdentity();
camera.setPerspective();
glTranslatef(camera.position.x, camera.position.y, camera.position.z);

b.draw();
}

//Draw Actors based on proximity
for(int p = 90; p >= 0 ; p -= 5){
for each(shared_ptr<Actor> a in actors){

GLfloat distance = (GLfloat) abs((a->position - camera.position).getLength());
if(distance > p - 5 && distance < p){
a->draw(camera);
}
}
}

return true;
}

void World::addActor(shared_ptr<Actor> a)
{
actors.push_back(a);
}[/code]
0

Share this post


Link to post
Share on other sites
Unfortunately, you've gone to the mechanic with a serious engine problem, but all you show him is the air freshener.

We need to see your code. All of it. Use a service like pastebin or something if it's too big to just dump into a post here.
0

Share this post


Link to post
Share on other sites
It turns out that this problem was a result of using the free version of Visual C++. I took my program to my professor and he recommended getting Visual Studio 2010 Professional. (Which I can get for free through Purdue!) I recreated my project in VS and the problem went away. That doesn't mean there weren't major memory issues with my program originally... Anyway thanks to all who helped me out with this! Now I can move forward!

Scott Wilkewitz
0

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1315616405' post='4859840']
I will bet you a nice beer that your problem did not stem from using Visual C++ Express, nor has it actually gone away (assuming you didn't change the code any).
[/quote]

This.

There are no 'memory limits' on classes, and using Visual C++ Express has nothing to do with it.

The problem is [b]in your code[/b] but for now you have papered over the cracks so it looks fine.

And people wonder why we don't recommend people use C++ around here...
0

Share this post


Link to post
Share on other sites
[quote name='swilkewitz' timestamp='1315612444' post='4859808']
I took my program to [b]my professor[/b] and he recommended getting Visual Studio 2010 Professional.
[/quote]

Wha???
0

Share this post


Link to post
Share on other sites
[quote name='laztrezort' timestamp='1315618163' post='4859848']
[quote name='swilkewitz' timestamp='1315612444' post='4859808']
I took my program to [b]my professor[/b] and he recommended getting Visual Studio 2010 Professional.
[/quote]

Wha???
[/quote]

Pretty much my reaction. The only way this recommendation would make any sense might be better ways of debugging memory issues with the Professional version.

Also keep in mind that even professors teaching computer science can be worthless as programmers (pointing out that computer science isn't programming). I could probably point at a few that are so deep into the theoretical side that they haven't touched any programming language for decades.
0

Share this post


Link to post
Share on other sites
[quote name='Trienco' timestamp='1315634721' post='4859894']
[quote name='laztrezort' timestamp='1315618163' post='4859848']
[quote name='swilkewitz' timestamp='1315612444' post='4859808']
I took my program to [b]my professor[/b] and he recommended getting Visual Studio 2010 Professional.
[/quote]

Wha???
[/quote]

Pretty much my reaction. The only way this recommendation would make any sense might be better ways of debugging memory issues with the Professional version.

Also keep in mind that even professors teaching computer science can be worthless as programmers (pointing out that computer science isn't programming). I could probably point at a few that are so deep into the theoretical side that they haven't touched any programming language for decades.
[/quote]

I don't understand your reactions. He teaches C and C++, and he solved my problem. His suggestion was spot on.
0

Share this post


Link to post
Share on other sites
No, his suggestion was ridiculous.


Think of it this way. You have major problems with recurring migraines. You go to the doctor. He suggests you shoot yourself in the head with a shotgun.

Hey, wow! You don't have migraines anymore!


Was his suggestion "spot on" or ludicrous?
2

Share this post


Link to post
Share on other sites
[quote name='swilkewitz' timestamp='1315698821' post='4860155']
I don't understand your reactions. He teaches C and C++, and he solved my problem. His suggestion was spot on.
[/quote]

No, he didn't solve anything.

This is like noticing you have a crack in a wall, you go to a builder and they put wallpaper over it. Sure you can't see the crack any more but that's not going to help you when, in the future, your house falls down.
2

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  
Followers 0