Jump to content
  • Advertisement
Sign in to follow this  
leggyguy

An odd problem - works if not pointer/doesn't if it is one

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am creating some classes for loading furniture into my 3d engine. Here's my problem:
      FurnitureItem FI;
      FI.PassName ("models/chair.3ds");
      FI.Init ();
Now the above works.
     FurnitureItem *Furniture = new FurnitureItem[10];

     Furniture[0].PassName ("models/chair.3ds"); 
     Furniture[0].Init ();
This doesn't work. It compiles, but gives me an "encountered problem and will close" pop-up when this part of the code runs. I have done this kind of thing in so many programs I couldn;t possibly count the times, but this time there is the problem. Now, I realize it may help you to see the code that these calls are pointing to, but I wondered if anyone had any idea's off the top of their head what may be happening here.

Share this post


Link to post
Share on other sites
Advertisement
What does this line do?

FurnitureItem *Furniture = new FurnitureItem[10];

try either

FurnitureItem *Furniture = new FurnitureItem();

or

FurnitureItem **Furniture = new FurnitureItem*[10];
for(int i=0; i<10; ++i)
Furniture = new FurnitureItem();


Share this post


Link to post
Share on other sites


FurnitureItem **Furniture = new FurnitureItem*[10];
for(int i=0; i<10; ++i)
{
Furniture = new FurnitureItem();
}
Furniture[0]->PassName ("models/chair.3ds");

Furniture[0]->Init ();



This still gives me the problem. The line I used in my code which you queried is something I have done fairly often in programs, and while it may not always be the correct way to do things, it has always worked fine.

But anyway, on this occasion, as I say, I get the problem with the above code to.

Share this post


Link to post
Share on other sites
It sounds like you're corrupting memory in either the PassName() or Init() function. When you create a non-pointer object, you're creating it on the stack, and I guess the stack is a little more lenient about corrupting memory. However, when you create it on the heap using new, you're then corrupting heap memory, which Windows is a little less happy about.

Share this post


Link to post
Share on other sites
There is not anything wrong at all with the new array declaration you're using so far as I can see.

Can you determine what line is actually breaking? Are there uninitialized variables that could be causing problems, or character arrays that aren't large enough and could result in overwrites?

-fel

Share this post


Link to post
Share on other sites
The code snippet is completely correct, try stepping through the PassName() and Init() functions with a debugger to see exactly where you get the error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!