array delete problem

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
elfuz
Posts: 14
Joined: Wed Oct 13, 2010 10:20 am

array delete problem

Post by elfuz »

hi all

i'm having problem with this block of code. It seems to generate a crash and i cannot seem to find it...

Code: Select all

  for( u32 k = 0; k < items_.size(); k++ )
   {
	   printf("%s: %i\n",items_[k]->TexFileName,items_[k]->UsageCount);
	   if (items_[k]->UsageCount == 0)
	   {
		   if (items_[k]->Texture) {
			   items_[k]->Texture->drop();
		   }
		   delete items_[k];
		   items_.erase(k);
	   }
   } 
the array consists of the TextureManagerItem defined here:

Code: Select all

   array< TextureManagerItem* > items_; 

Code: Select all

  class TextureManagerItem
   {
   public:
      ITexture* Texture;
	  u32		TexID;
	  stringc	TexName;
	  stringc	TexFileName;
	  u32		UsageCount;
   }; 
thanks for any help

greet
elfuz
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

don't call erase() inside the loop but call clear() after the loop. With every iteration the array becomes smaller so after you erased enough elements from the array you are trying to access elements that are no longer there.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Metalero
Posts: 14
Joined: Tue Sep 07, 2010 4:02 pm

Post by Metalero »

I think I had same problem in the past.

Try doing a

Code: Select all

k--;
below the part you erase the array item, coz remember, in this part, the array made samller, but k continue groing up.
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

You should learn how to use your debugger.

Also, make sure that the textures you drop() aren't used by Irrlicht anymore. Your program will definitely crash, if you try to remove a texture currently in use.
Never take advice from someone who likes to give advice, so take my advice and don't take it.
elfuz
Posts: 14
Joined: Wed Oct 13, 2010 10:20 am

Post by elfuz »

i've tried everything however the code keeps crashing on me :(

i'm trying to use the debugger, however it points to crt0.c during the crash and i cannot analyze anything from the array (which i can if the crash occurs in one of my source files)

Code: Select all

        return __tmainCRTStartup();
Metalero
Posts: 14
Joined: Tue Sep 07, 2010 4:02 pm

Post by Metalero »

Use CallsStack to go back to your code (what IDE are you using ?)
elfuz
Posts: 14
Joined: Wed Oct 13, 2010 10:20 am

Post by elfuz »

VC++ 2010 Express
Nalin
Posts: 194
Joined: Thu Mar 30, 2006 12:34 am
Location: Lacey, WA, USA
Contact:

Post by Nalin »

Sylence wrote:don't call erase() inside the loop but call clear() after the loop. With every iteration the array becomes smaller so after you erased enough elements from the array you are trying to access elements that are no longer there.
This is the correct idea. If you are deleting the entire array, delete all the elements then clear() the array. It makes no sense to remove each individual element from the array when you can do the same thing with a single clear().

Also, if you are trying to selectively remove multiple items from a single for loop, you will want to start at the end and work your way down, or call --k after you delete an item. The reasoning behind this is that if you delete element 0, elements 1..n will be shifted down, so element 1 becomes the new element 0. When k is incremented in the for loop, you now erase element 1, which was previously element 2. You have now skipped the old element 1, which became element 0. Walking backwards through the array solves this, as well as subtracting 1 from k when you delete an item.
elfuz
Posts: 14
Joined: Wed Oct 13, 2010 10:20 am

Post by elfuz »

hmm that makes sense :-D

thanks, i'll give that a whirl.
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Post Reply