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

Started by
5 comments, last by Luctus 19 years, 6 months ago
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.
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();


Don't shoot! I'm with the science team.....
	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.
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.
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
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
ya, post the code to your FurnitureItem class (use [*source][/source] tags around it, no asterix)

-me
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.
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams

This topic is closed to new replies.

Advertisement