Jump to content

  • Log In with Google      Sign In   
  • Create Account

Odd memory problems with classes


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
45 replies to this topic

#1 swilkewitz   Members   -  Reputation: 128

Like
0Likes
Like

Posted 29 June 2011 - 08:17 AM

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...

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
}


#2 smasherprog   Members   -  Reputation: 568

Like
0Likes
Like

Posted 29 June 2011 - 10:44 AM

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.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#3 ApochPiQ   Moderators   -  Reputation: 20451

Like
0Likes
Like

Posted 29 June 2011 - 12:19 PM

Agreed - it sounds like you're smashing memory someplace. The primary suspects are anywhere you use raw pointers; shared_ptr is not likely to be the culprit.
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#4 swilkewitz   Members   -  Reputation: 128

Like
1Likes
Like

Posted 29 June 2011 - 08:07 PM

Oh, thanks! Hmm...

". . . areas of your program where you use pointers to write stuff to."
Where I am writing to 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

#5 Hodgman   Moderators   -  Reputation: 48431

Like
0Likes
Like

Posted 29 June 2011 - 08:18 PM

How would I accidentally "smash" memory? Could you give me an example of what to look for?

Usually the culprits are bad loops (looping too many times), or invalid casts (casting a pointer to the wrong type).
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'

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


#6 D.Chhetri   Members   -  Reputation: 181

Like
0Likes
Like

Posted 29 June 2011 - 08:55 PM

Oh good old buffer overflow. I can't believe you guys thought of that. I hate smart people.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

#7 BitMaster   Crossbones+   -  Reputation: 8495

Like
0Likes
Like

Posted 30 June 2011 - 02:16 AM

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.

#8 swilkewitz   Members   -  Reputation: 128

Like
0Likes
Like

Posted 30 June 2011 - 09:07 AM

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.

So did I find the final solution, or did I just find another bypass?
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

#9 swilkewitz   Members   -  Reputation: 128

Like
0Likes
Like

Posted 30 June 2011 - 09:37 AM

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!

#10 ApochPiQ   Moderators   -  Reputation: 20451

Like
0Likes
Like

Posted 30 June 2011 - 11:06 AM

Try posting your code for review.
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#11 BuffaloJ   Members   -  Reputation: 142

Like
0Likes
Like

Posted 30 June 2011 - 11:17 AM

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


#12 swilkewitz   Members   -  Reputation: 128

Like
0Likes
Like

Posted 01 July 2011 - 09:49 AM

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


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.

#13 ApochPiQ   Moderators   -  Reputation: 20451

Like
0Likes
Like

Posted 01 July 2011 - 10:49 AM

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 (not members of your classes but entire pieces of functionality) until you have a minimal program that reproduces the issue. Then you can post that.
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#14 BuffaloJ   Members   -  Reputation: 142

Like
0Likes
Like

Posted 01 July 2011 - 10:58 AM

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...





#15 ApochPiQ   Moderators   -  Reputation: 20451

Like
0Likes
Like

Posted 01 July 2011 - 11:06 AM

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.
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#16 swilkewitz   Members   -  Reputation: 128

Like
0Likes
Like

Posted 02 July 2011 - 09:31 PM

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:

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;
}

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);
}


#17 ApochPiQ   Moderators   -  Reputation: 20451

Like
0Likes
Like

Posted 03 July 2011 - 01:19 AM

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.
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#18 swilkewitz   Members   -  Reputation: 128

Like
0Likes
Like

Posted 09 September 2011 - 05:54 PM

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

#19 ApochPiQ   Moderators   -  Reputation: 20451

Like
4Likes
Like

Posted 09 September 2011 - 07:00 PM

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).
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#20 phantom   Members   -  Reputation: 10414

Like
0Likes
Like

Posted 09 September 2011 - 07:17 PM

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).


This.

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

The problem is in your code 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...




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS