Okey dokey, I've tried this a million times before and just give up because I can't figure it out. Basically in my Cvar manager class I'm using a std::map, mapping names to Cvar*'s. Everything else works fine, I just want to free some memory. Each Cvar has an strField, which may or may not be allocated depending on what the caller wants to store in it. The Cvar itself is also allocated.
Now, this cleanup routine seems to work (it didn't crash at least):
Code:
void CvarMgr::Cleanup()
{
CvarMapType::iterator it = _cvars.begin();
CvarMapType::iterator ite = _cvars.end();
for ( ; it != ite; it++ )
{
if ( (*it).second->strField )
delete[] (*it).second->strField;
delete (*it).second;
}
}
But, with my Set routine, which modifies the values of an existing Cvar, I can't delete[] strField without one of those "damaged block" runtime dialogs.
Code:
/* Set
* Sets the values of an existing variable. This may be a little
* awkward to call. Should probably revise it.
*/
bool CvarMgr::Set( const std::string& name, ULONG ul, long l, float f, double d, BYTE by, bool b, const char* str )
{
CvarMapType::iterator it;
if ( (it = _cvars.find(name)) == _cvars.end() )
{
ErrorLogging::Log( Va("Cvar '%s' doesn't exist (trying to set it)", name.c_str()), true );
return false;
}
(*it).second->bField = b;
(*it).second->byField = by;
(*it).second->dField = d;
(*it).second->fField = f;
(*it).second->lField = l;
(*it).second->ulField = ul;
if ( str )
{
if ( (*it).second->strField )
{
delete[] (*it).second->strField; // Dies right here
(*it).second->strField = new char[strlen(str)];
sprintf( (*it).second->strField, "%s", str );
}
else
{
(*it).second->strField = new char[strlen(str)];
sprintf( (*it).second->strField, "%s", str );
}
}
return true;
}