#### Archived

This topic is now archived and is closed to further replies.

# STL Vector heap accessing error

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

## Recommended Posts

Hey, Just started recently getting familiar with STL, and it seemed pretty easy, and the uses quickly became obvious. So, for my first try at implementing vectors, I wanted a dynamic list of textures that I could add to for my editor. vector TextureList; (in the after the file is opened...) TextureList.push_back(CTexture()); Anyway, i get a heap access error, which is actually a failing assert in micro$softs dbgheap.c file. I''m wondering if there is a specific way that I need to tell my program that I want to specifically access the ''local'' heap, so as not to try to access any bogus memory. Thanx "With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I''m reaching up and reaching out. I''m reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one''s been. We''ll ride the spiral to the end and may just go where no one''s been." - Maynard James Keenan Name: TheBlackJester Team: Wildfire Studios Projects O A.D. The Last Alliance #### Share this post ##### Link to post ##### Share on other sites Advertisement if i had a company that was really successful, i''d love it if people spelled the name with a dollar sign rather than an s. are you chipping away at something? #### Share this post ##### Link to post ##### Share on other sites Ok, I try to ask a legitamate, mature question. I use a common, sarcastic mispelling of a generally (not unanimously) disliked company, and you waste my, and everyone elses who will read this thread's time by commenting on such subtleties.... I'm sorry. [EDIT:] Sorry fruny, you replied while I was replying to petewood. Thanks for the useful reply . Unfortunately thats not the problem though. I'm not trying to access any part of the vector, rather, i'm just trying to push_back one element. "With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I'm reaching up and reaching out. I'm reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one's been. We'll ride the spiral to the end and may just go where no one's been." - Maynard James Keenan Name: TheBlackJester Team: Wildfire Studios Projects O A.D. The Last Alliance [edited by - TheBlackJester on December 7, 2002 3:28:38 PM] #### Share this post ##### Link to post ##### Share on other sites i'm also helping keep your thread at the top of the list. edit: i think something that moved me to comment was the fact that you refered to microsoft in such a way as to imply that their desire for money was related to a crash in the dbgheap.c file. maybe you weren't but i reacted. anyway, it's quite unlikely that your crash has anything to do with a problem in the library implementation. show us more code please. also putting a & followed by lt; for a less than sign and & followed by gt; for greater than will sort out your formatting sorry to waste your time. lets try to get your problem sorted out. pete [edited by - petewood on December 7, 2002 4:08:23 PM] #### Share this post ##### Link to post ##### Share on other sites Another possibility would be that your CTexture object holds a pointer, deleted in the destructor, and you didn't define a copy constructor. Thus, when the temporary object created by CTexture() gets destroyed, so does the data. Or something... Though it shouldn't cause a problem here... Hmm... I'm afraid you're going to have to examine your call stack and the history of the operations done on that vector. BTW - petewood, according to Ars Technica, it's 'Microshizzle' [ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | Free C++ IDE. ] [ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Boost C++ Lib ] [edited by - Fruny on December 7, 2002 4:16:54 PM] #### Share this post ##### Link to post ##### Share on other sites quote: Original post by TheBlackJester I try to ask a legitamate, mature question. yes, and you ruin it by quote: a common, sarcastic mispelling of a generally (not unanimously) disliked company don''t like ms software? don''t use it. you may not realize it, but spelling ms with$ puts you in the camp of the brainless ms bashers that blame all their bugs on ms and think they are cool because of that.

i don''t want to help people who dislike environment they are working in with environment-related because i think they just paste code related to said environment without thinking much about it.

ms debug heap provides more information than "a failing assert", which is all you gave us. if you weren''t as ignorant as you are, you could''ve fixed or at least localized the problem yourself by using the tools of your environment, such as the call stack, by looking at the source code in dbgheap.c that breaks, as well as reading the comments and error message produced by the c runtime.

##### Share on other sites
Petewood: Apology accepted, thank you. I'll try out the suggestion you gave, thanks a bunch. I didn't mean to sound like I was blaming microsoft for my issue, and I'm sure there is no problem with the dbgheap.c file. I simply wanted to let you guys know where the assert was failing, because I thought it might give a little insight into the problem. I apologize for that.

Fruny: I believe the problem lies where you mentioned, in my CTexture file. You mention that I may need a copy constructor, and I'm sure that that could likely be the problem, but considering how i'm not completely familiar with the term or how to implement it, I'll do some research

Niyaw: ... Hmm. Normally, I would respond to your post with an apology, just like with Petewood, but your tone struck me as degrading, conceded, and pretty much unnessassary. So...

quote:

don't like ms software? don't use it.

As you may have noticed, I wrote, "a generally disliked company ". Not software. I enjoy microsoft software very much, and as I pointed out to petewood, I was not trying to blame them for my problem, but give insight into what might be causing it.

quote:

but spelling ms with \$ puts you in the camp of the brainless ms bashers that blame all their bugs on ms and think they are cool because of that.

Well, its pretty sad that you can assume so much information about me (ie, brainless, blaming my bugs on MS, thinking I'm cool because of it.) by a simple sarcastic misspelling. I don't know if YOU realize how much money microsoft actually has, which that in itself would warrent a sarcastic misspelling such as the afformnetioned, without any negative assumptions about microsft whatsoever.

quote:

i don't want to help people who dislike environment they are working in with environment-related because i think they just paste code related to said environment without thinking much about it.

As i mentioned before, I enjoy microsoft products, and thus my development environment, very much. No code was pasted related to any "said envrionment" whatesoever so I have no idea what your talking about there.

quote:

ms debug heap provides more information than "a failing assert", which is all you gave us. if you weren't as ignorant as you are, you could've fixed or at least localized the problem yourself by using the tools of your environment, such as the call stack, by looking at the source code in dbgheap.c that breaks, as well as reading the comments and error message produced by the c runtime.

Well, about my being ignorant, your intirely correct in this, and I apologize for that. My knowledge in the use of the call stack, the debugger, and other features of MSVC++ are quite lacking, and again I'm sorry that I couldn't corrrectly utilize them before having to inquire about my problem on the boards.

Here is my CTexture class.

  class CTexture{public:	unsigned int	m_ID;					// Texture object ID	unsigned char	*m_imageData;			// Image data	short int		m_imageWidth;			// Image Width	short int		m_imageHeight;			// Image Height	unsigned char   m_imageTypeCode;		// RGB or RGBA	unsigned char	m_bitCount;				// 16, 24, or 32	bool m_mipMap;						// Do we want to mipmap this texture?	int m_minFilter;					// OpenGL GL_TEXTURE_MIN_FILTER	int m_magFilter;					// OpenGL GL_TEXTURE_MAG_FILTER	int m_textureWrapS;	int m_textureWrapT;	int m_textureEnvMode;	// This is the function to load a TGA	int LoadTGA(char *fileName, 		        int filterMin = GL_LINEAR, 				int filterMag = GL_LINEAR,				int textureWrapS = GL_REPEAT,				int textureWrapT = GL_REPEAT,				int textureEnvMode = GL_MODULATE);	// Constructor	CTexture()	{	};	// Destructor	~CTexture()	{		delete m_imageData;	};};

Thanks for taking the time

"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I'm reaching up and reaching out. I'm reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one's been. We'll ride the spiral to the end and may just go where no one's been." - Maynard James Keenan
Name: TheBlackJester
Team: Wildfire Studios

Projects
O A.D.
The Last Alliance

[edited by - TheBlackJester on December 7, 2002 5:20:02 PM]

##### Share on other sites
The problem indeed lies in the lack of copy constructor. Or at least one huge problem. Vector copies the objects around when it''s size changes and when you put stuff there.. Your destructor deletes the dynamically allocated data so when the class is copied (without custom copy constructor), it gets deleted twice.

Simple example of what can happen:

  void jesus(CTexture a) {  //do something}int main() {  CTexture x;  jesus(x);}

Now x has all the stuff allocated. When it is passed to ''jesus'', the class is copied (it''s all data members - pointers and all). When jesus stops executing, it calls the destructor of CTexture for object ''a''. It deletes m_imageData. Now we return from the function. ''x'' points to the same m_imageData that ''a'' used to point, but ''a'' deleted it! Uh oh. When ''x'' goes out of scope, m_imageData gets deleted again.

Since CTexture is such a huge class, I suggest you use a vector of pointers (or smart pointers) instead:

  vector<CTexture*> TextureList;TextureList.push_back(new CTexture);

Just don''t forget to delete those objects eventually (or use smart pointers).

And if you don''t want to make proper copy constructors, at least declare copy constructor and assignment operator as private so that nobody can call them by accident.

  class CTexture{//...private:  CTexture(const CTexture&);  CTexture& operator=(const CTexture&);}

##### Share on other sites
Hey,

If I used pointers for my texture objects, would that still require manuel deletion, even though vectors take care of that for you?

"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I''m reaching up and reaching out. I''m reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one''s been. We''ll ride the spiral to the end and may just go where no one''s been." - Maynard James Keenan
Name: TheBlackJester
Team: Wildfire Studios

Projects
O A.D.
The Last Alliance

• 16
• 9
• 13
• 41
• 15