[bug] ObjectDelete

 

When deleting in cycle objects


         int total_objects = ObjectsTotal(chartid);
         for(int o=0; o<total_objects; o++){
            string name = ObjectName(chartid, o);
            if (is_numeric(StringSubstr(name, 0, 1))){
               ObjectDelete(chartid, name);
            }
         }


not all objects are deleted, for example in the object list table is more than 100 objects, and I trying to delete 50 objects by its name, its first symbols must be numeric. After I run the code, only ~30 objects are deleted, after I rerun code again, 20 objects also deleted.

So I decide that ObjectDelete has a core bug.

 
waza123:

When deleting in cycle objects


         int total_objects = ObjectsTotal(chartid);
         for(int o=0; o<total_objects; o++){
            string name = ObjectName(chartid, o);
            if (is_numeric(StringSubstr(name, 0, 1))){
               ObjectDelete(chartid, name);
            }
         }


not all objects are deleted, for example in the object list table is more than 100 objects, and I trying to delete 50 objects by its name, its first symbols must be numeric. After I run the code, only ~30 objects are deleted, after I rerun code again, 20 objects also deleted.

So I decide that ObjectDelete has a core bug.

Try this way

int total_objects = ObjectsTotal(chartid);
         for(int o=total_objects-1; o>=0; o--){
            string name = ObjectName(chartid, o);
            if (is_numeric(StringSubstr(name, 0, 1))){
               ObjectDelete(chartid, name);
            }
         }
 
Wahoo:

Try this way

Wahoo has given you exactly the code you want.  The reason is you have fallen into a trap which all programmers fall into (usually once)!  When deleting objects it is advisable to delete from the last back to the first, not the first through to the last.  If you have 100 objects indexed 0 to 99 and delete the first (the zero numbered element) you will commonly be left with 99 objects now indexed 0 to 98 - and as your code is now about to delete number 1 it will never get back to delete the new number 0.  As well as this when your code gets half-way through there will be no objects left numbered 51 through to 100 so you're likely to get a series of overrun type errors as well!

John

 
thanks
 
jftjft:

Wahoo has given you exactly the code you want.  The reason is you have fallen into a trap which all programmers fall into (usually once)!  When deleting objects it is advisable to delete from the last back to the first, not the first through to the last.  If you have 100 objects indexed 0 to 99 and delete the first (the zero numbered element) you will commonly be left with 99 objects now indexed 0 to 98 - and as your code is now about to delete number 1 it will never get back to delete the new number 0.  As well as this when your code gets half-way through there will be no objects left numbered 51 through to 100 so you're likely to get a series of overrun type errors as well!

John

Very good explanation. I only want to add that this is true not specifically for deleting objects, but also for Closing Positions/orders, Deleting Global Variables, Processing Available symbols etc. Basically everything which is done in a loop. 
Reason: