-- [ Picked text/plain from multipart/alternative ] I can't really give a code snippet because for it to be in any kind of context you would need to see quite a bit of it.
I will try and simplify it: I have a class, CItem. This class contains char arrays, integers, etc, etc holding data about a specific item.. the contents aren't that important - at no point anywhere in this class do i create any memory with new or malloc. I then have another class, CInvItem, which consists of three CItem pointers (because in this system items are combined with others randomly to generate a large armount of different items). Three CItem pointers are passed on construction, and copied to the member pointer variables. In the destructor I have delete calls for all three pointers. Then I have my CInventory class. Among other things, this contains a CUtlVector<CInvItem*> m_vItems. In the CInventory constructor I add a specific number of "slots" to the vector using AddToTail( NULL ). Any items added to the inventory by other functions use m_vItems[slot] = new CInvItem( CItem*, CItem*, CItem* ). The pointers for this constructor are retrieved from a class called CItemData which loads all items from a data file into a CUtlVector<CItem*> before the call to new CInvItem. The CInventory destructor calls delete on all m_vItems elements, before emptying the vector. The CItemData destructor does the same for its vector of CItem*. Now this crash only occurs when I have an item in the inventory.. and only when I load a new map using "map mapname". Using engine->ChangeLevel from within the code (related to a logical entity I have created) does not crash the game. I'm quite puzzled as to why it only crashes when I manually change the map in the console, but when I do it gives me the nice memory error popup saying that location "x" could not be "read". On 1/11/08, Ronny Schedel <[EMAIL PROTECTED]> wrote: > > > Give me some code snippet to understand clearly your problem. Don't forget > declarations, etc. Just some short code which will crash (create one > element, add to vector, remove element from vector). > > > > > -- > > [ Picked text/plain from multipart/alternative ] > > That's what I thought... in that case I need to figure out why calling > > delete vector[element] crashes my mod! > > > > On Jan 11, 2008 8:07 PM, Yahn Bernier <[EMAIL PROTECTED]> > > wrote: > > > >> If the element type is a pointer: > >> > >> CUtlVector< CMyClass * > vecStuff; > >> > >> Then the underlying object will not be destructed, just the slot > holding > >> the ptr. > >> > >> If you do: > >> > >> CUtlVector< CMyClass > vecStuff; > >> > >> Then the object gets desctructed (but you also have to worry about > >> implementing a copy constructor or operator =, etc. etc.) > >> > >> Safest thing in the CUtlVector< CMyClass * > case is to loop through > the > >> objects and call delete on each entry, and then call Purge/RemoveAll to > >> free the memory used for the raw pointers. > >> > >> ~CUtlVector will automatically clean up the ptrs, but if you don't > >> delete the objects in the CUtlVector< CMyClass * > case then you'll > have > >> a leak. > >> > >> Yahn > >> > >> -----Original Message----- > >> From: [EMAIL PROTECTED] > >> [mailto:[EMAIL PROTECTED] On Behalf Of Ronny > >> Schedel > >> Sent: Friday, January 11, 2008 11:50 AM > >> To: hlcoders@list.valvesoftware.com > >> Subject: Re: [hlcoders] CUtlVector<*>... Memory management? > >> > >> Why you don't look at the code itself? In "Remove" you can see the > >> element > >> will be destroyed by the Destruct function. The Destruct function calls > >> the > >> Destructor of the element. > >> > >> Best regards > >> > >> Ronny Schedel > >> > >> > >> > -- > >> > [ Picked text/plain from multipart/alternative ] > >> > Am I right in assuming that if you have a vector of pointers, that > >> point > >> > to > >> > things you create with "new", you have to either call delete on each > >> > element > >> > or use PurgeAndDeleteElements()? Because that's what I've been using > >> up > >> > until recently, where it seems that trying to delete elements from a > >> > pointer > >> > vector on destruction of whatever they are members of just causes the > >> game > >> > to crash with a memory error. Removing the calls to delete stop the > >> crash, > >> > but unless CUtlVector automatically cleans up your memory for you, > >> won't > >> > this just create MASSIVE memory leaks? As far as I knew, CUtlVector > >> > didn't > >> > magically look after your memory for you... was I wrong? > >> > > >> > J > >> > -- > >> > > >> > _______________________________________________ > >> > To unsubscribe, edit your list preferences, or view the list > archives, > >> > please visit: > >> > http://list.valvesoftware.com/mailman/listinfo/hlcoders > >> > > >> > >> > >> _______________________________________________ > >> To unsubscribe, edit your list preferences, or view the list archives, > >> please visit: > >> http://list.valvesoftware.com/mailman/listinfo/hlcoders > >> > >> > >> _______________________________________________ > >> To unsubscribe, edit your list preferences, or view the list archives, > >> please visit: > >> http://list.valvesoftware.com/mailman/listinfo/hlcoders > >> > >> > > -- > > > > _______________________________________________ > > To unsubscribe, edit your list preferences, or view the list archives, > > please visit: > > http://list.valvesoftware.com/mailman/listinfo/hlcoders > > > > > _______________________________________________ > To unsubscribe, edit your list preferences, or view the list archives, > please visit: > http://list.valvesoftware.com/mailman/listinfo/hlcoders > > -- _______________________________________________ To unsubscribe, edit your list preferences, or view the list archives, please visit: http://list.valvesoftware.com/mailman/listinfo/hlcoders