Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with STL vector

Started by NovaCaine Sep 21, 2005 at 7:20 PM 2 replies 350+ views
Original Post
NovaCaine
NovaCaine
Hey all, Haveing a small problem with STL vectors. First of all, I'm using Visual C++ 2005 express Beta 2; and heres my code

void CRing::Create( LPDIRECT3DDEVICE9 p_pDevice, char* p_strModelName, int p_iNumOfItems, float p_fRadius ) {
	
	//CXModel* pTempModel;

	m_fRadius = p_fRadius;
	m_iNumOfItems = p_iNumOfItems;
	
	// for each 'item'
	for( int i = 0; i < p_iNumOfItems; i++ ) {
		// create a model
		CXModel* pTempModel = new CXModel();

		pTempModel->LoadXModel( p_strModelName, p_pDevice );

		// add to vector list
		m_vecModelList.push_back( pTempModel ); //<--- Problem seems to be here

		assert( m_vecModelList == pTempModel );

		pTempModel = NULL;
	}

	PositionModels();
}

The idea of all of this is that i want to create a ring of models, the placement of them seems to be working and they are rendering, as my model class is basically just the .X model class from DirectX samples. What my problem is that while looping thru, adding models to my vector, some of them seem to be filled with junk (addresses like 0xcdcdcdcd or 0xfeeefeee). I then stepped thru my code to fine that pTempModel was being filled correctly, but after i pushed it onto the vector list m_vecModelList it would have a crap address, pass the assert( unsure how this could be possible ), and seems to not give me any errors. Any insight into what the problem could be would be great.
ArmitageIII87
ArmitageIII87
The addresses 0xCDCDCDCD and 0xFEEEFEEE aren't junk addresses, there's a purpose to them...

http://www.nobugs.org/developer/win32/debug_crt_heap.html

Sorry, I don't know how to create a link in this edit box, and can't find it in the forum faq.
Shannon Barber
Shannon Barber
The only thing I see wrong with this code is that you assume the vector is empty. Either you should explictly .clear() it before entering the loop, or the assert is not checking the correct index. The assert doesn't matter though, the code looks good otherwise.

I think your problem is elsewhere. Just to check, post the declaration of m_vecModelList.


PS You need to iterate the vector and delete everything you put in it before you clear it, unless you are using smart-pointers.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
NovaCaine
NovaCaine
my declaration of the vector is

std::vector<CXModel*>	m_vecModelList;


this is in the header file of a class i am creating

also i didn't mean that the addresses were junk as such, more that they were not the addresses of the pointer i was pushing into the vector.

the function i posted is the first function that gets called in this class, apart from initialising some variables for DirectX( and now clearing the vector ), nothing happens till this function.

here is an image of me running thru a debug. it has been stopped after the second model has been created and loaded. As you can see, the pTempModel pointer has a valid addres and looks to be filled correctly, but in the second index of the vector, it does not equal the same address.

http://www.geocities.com/novacaineindustries/Problem.jpg

PS:
Quote:

PS You need to iterate the vector and delete everything you put in it before you clear it, unless you are using smart-pointers.


i dont think i am using smart pointers, but i am deleting everything before i clear it in my Destroy function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.