Sign in to follow this  
j_smith4

Problem Creating New Objects

Recommended Posts

I've got a pointer problem or something. I'm storing the game objects to render in a vector like: vector<Object *>objects and just pushing them back. Maybe I'm to used to Java know but I'm creating new ships like this: objects.push_back(new Ship(...)). The first object I push on renders fine but the rest done. Now if I just used vector<Object> objects and just used the manual created for the ship and push it on all of them render. The real kicker is when running in debug model pushing new objects on works fine. What I can do is something like this:
std::vector <Ship *> ships;
Ship *b;

b = new Ship();
b->Create(renderer.GetDevice(), &model, D3DXVECTOR3(100,0,50));
ships.push_back(b);

b = new Ship();
b->Create(renderer.GetDevice(), &model2, D3DXVECTOR3(-100,0,50));
ships.push_back(b);

Ship does have a constructor that takes the Create arguments and just passes them through to the Create function but using that method is what is giving me the problems.

Share this post


Link to post
Share on other sites
vector.push_back(new Ship()); works, if vector is correctly initialized, the constructor of Ship is sane, and the function which accesses the vector is correct. If your code doesn't work, then you're doing something wrong with either of these things. What does the debugger tell you about the access function in the release version, and the state of the vector when it executes?

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
vector.push_back(new Ship()); works, if vector is correctly initialized, the constructor of Ship is sane, and the function which accesses the vector is correct. If your code doesn't work, then you're doing something wrong with either of these things.


What makes it weird is push_back(new Ship()) works fine when the game runs in debug mode. All the constructor does is zero out class data and then call the create function passing on the parameters given to the constructor. Does debug mode run in some kind of safe mode with memory?

Share this post


Link to post
Share on other sites
Quote:
Original post by j_smith4
What makes it weird is push_back(new Ship()) works fine when the game runs in debug mode. All the constructor does is zero out class data and then call the create function passing on the parameters given to the constructor. Does debug mode run in some kind of safe mode with memory?


Yes, debug mode often cleans up allocated memory before giving it to your code, or similar modifications. It also enforces some level of overrun protection by inserting sentinels.

Share this post


Link to post
Share on other sites
I'm not quite sure what the problem is/was. But each Ship class also has a Model class. What I was doing is creating all the Models at the beginning and then passing a pointer to whatever Model that ship needed. But the Ship Model wasn't a pointer and I was passing it by reference (&). Now that I changed it to a pointer it seems to be working alright.

Anybody have an idea why debug mode let it slide but release wouldn't?

Share this post


Link to post
Share on other sites

b->Create(renderer.GetDevice(), &model, D3DXVECTOR3(100,0,50));
ships.push_back(b);

b = new Ship();
b->Create(renderer.GetDevice(), &model2, D3DXVECTOR3(-100,0,50));
ships.push_back(b);


what type is model1/model2
and what is the signature for Ship::Create

Share this post


Link to post
Share on other sites
Quote:
Original post by fishmd
what type is model1/model2
and what is the signature for Ship::Create


I changed it to use pointers and now I don't remember what I had.


Model mode1;
model1.Create(...);

Ship::Create(IDirect3DDevice9 *p_D3DDevice, Model *model, D3DXVECTOR3 position = D3DXVECTOR3(0,0,0));


Before the Model in the ship was just a regular member variable and not a pointer. I was passing in the address of the model to Create and so when the function that made the ships exited model was pushed off. But with that old method the first ship I put into the vector would render properly.

Share this post


Link to post
Share on other sites
well if you did something like this:


void makeShips()
{
Model modelA;
Ship* b = new Ship()
b->create(dev, &modelA, etc)
vect.push_back(b);
}




and you stored your model inside the ship as a pointer, the model pointer will become a dangling reference as soon as that function's call stack collapses. You should allocate the model dynamically or within some sort of class that contains all your models.

Share this post


Link to post
Share on other sites
Thats basically what I did. When the game starts the ship models are new'd. Then the Ship class now contains a pointer to a Model class and the pointer to the model is passed into the Ship class when created.

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