Jump to content
  • Advertisement

Archived

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

NullSeraph

Question about STL

This topic is 6130 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 just recently started using STL and I'm starting to have some really strange problems. I have a struct defined like this. struct SMemBlock { void *Address; unsigned long Line; CString Filename; unsigned long Size; }; I use this struct inside a vector like so. typedef vector < SMemBlock > TMemRefArray; TMemRefArray MemTracker; Now whenever I remove an element from the vector I get an assertion. I delete the memory that Address points to before removing the element, and I think that might be causing it. My question is, does STL automatically try to delete every element of a struct/class/etc when you use it as the base for a vector. And if that's true, is there anyway to stop it from doing that? This is based off a memory tracking routine I read in an article, and it works fine the way they implement it. I'm also using MSVC 6.0 if that matters. Edited by - DoomBunny on February 4, 2002 8:20:50 PM

Share this post


Link to post
Share on other sites
Advertisement
The contains won''t delete pointers unless you tell them to - I think it will invoke the dtor of SMemBlock when you call erase (to actually remove something from a container, you call remove and then erase).

Post the code that asserts

I''d use std::string instead of CString

Share this post


Link to post
Share on other sites
  
inline void __cdecl operator delete (void *Pointer)
{
TMemInfoList::iterator Iterator;

free (Pointer);

for (Iterator = MemInfoList.begin ();Iterator != MemInfoList.end ();Iterator++)
{
if ((*Iterator).Address == (DWORD) Pointer)
{
MemInfoList.erase (Iterator);
break;
}
}
};


This is the code that causes the assert. It happens right at the MemInfoList.erase call.


Edited by - DoomBunny on February 4, 2002 10:15:26 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When a copy constructor or assignment operator is not defined for a struct or class, the compiler generates one that just does a dumb memory copy. Inside SMemBlock, you have a complex object, the CString, that keeps a pointer to its internal memory buffer. The vector does some kind of assignment internally that essentially creates a clone of the SMemBlock including the buffer pointer. When this internal clone is destroyed, the pointer is freed leaving the original hanging out there. When you go to destroy the original, it tries to delete the pointer that was already freed, thus giving you the assertion.

There are two ways around this. The simple way is to just add a copy constructor and assignment operator to SMemBlock that just assigns to each member (this->Address = r.Address, etc.). The slightly more complicated, but probably much better, approach is to create a vector of SMemBlock*.

Share this post


Link to post
Share on other sites
Thanks for the help, everyone. Anon''s suggestion coincides with the source code that the article provided, so I understand why they used pointers instead of the structures now.
Also, I created that CString class as an exercise in string manipulation, so I have a bad habit of using it wherever I can just to test its reliability. Anyway, I''m going to use anon''s idea and switch it to pointers.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!