Question about STL

Started by
4 comments, last by NullSeraph 22 years, 2 months ago
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
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
- 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
  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
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*.
The filename can be stored using a const char*, you don''t need CString for that.
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.

This topic is closed to new replies.

Advertisement