issue #343 - was purging the wrong model from cache, causing pointers to freed memory

This commit is contained in:
Timothee "TTimo" Besset 2016-04-24 16:50:11 -05:00
parent cc68a76900
commit 376eb6c0ac
3 changed files with 138 additions and 133 deletions

View file

@ -20,6 +20,7 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include "entity_entitymodel.h" #include "entity_entitymodel.h"
#include "entity.h" #include "entity.h"
@ -201,9 +202,11 @@ void CEntityMiscModel::SetName( const char *name ){
return; return;
} }
if ( m_cachereq.GetBuffer()[0] != ':' if ( m_oldcachereq.GetBuffer()[0] != ':'
&& m_version.c_str()[0] != '\0' ) { && m_version.c_str()[0] != '\0' ) {
GetModelCache()->DeleteByID( m_cachereq.GetBuffer(), m_version.c_str() ); GetModelCache()->DeleteByID( m_oldcachereq.GetBuffer(), m_version.c_str() );
} else {
assert(m_model == NULL);
} }
m_model = NULL; m_model = NULL;

View file

@ -251,10 +251,12 @@ virtual void DeleteByID( const char *id, const char* version ){
{ {
elem = (CModelWrapper*)m_ptrs->pdata[i]; elem = (CModelWrapper*)m_ptrs->pdata[i];
if (strcmp(elem->m_version.c_str(), version) == 0 if (strcmp(elem->m_version.c_str(), version) == 0
&& strcmp( elem->m_id.c_str(), id ) == 0 && strcmp(elem->m_id.c_str(), id) == 0) {
&& --elem->refcount == 0 ) { elem->refcount--;
if (elem->refcount == 0) {
g_ptr_array_remove_index_fast(m_ptrs, i); g_ptr_array_remove_index_fast(m_ptrs, i);
delete elem; delete elem;
}
return; return;
} }
} }