Foreach saferemove causes infinite loop
#1

Hey

Im using foreach 19 (0.4.3) and removing iterators from loop causes crash.

i followed the instuctions asthis topic says but the problem is still alive and sometimes this function causes this error too:

Код HTML:
[2017/10/01 16:22:20] [debug] Run time error 4: "Array index out of bounds"
[2017/10/01 16:22:20] [debug]  Accessing element at index 1000 past array upper bound 999
and this is my code:

Код HTML:
	foreach(new serveritemid : ServerItems)
	{
		count[serveritemid]++;
		if(count[serveritemid] == 3)
		{
			new Prev = Iter_Prev(ServerItems, serveritemid);
			printf("remove %d || Prev: %d", serveritemid, Prev);
			Iter_SafeRemove(ServerItems, serveritemid, Prev);
		}
		if(!Iter_Count(ServerItems))
		{
			printf("ServerItems empty");
		}
		printf("%d", serveritemid);
	}
output:
Код HTML:
[2017/10/01 17:19:30] 0
[2017/10/01 17:19:30] 1
[2017/10/01 17:19:30] 2
[2017/10/01 17:19:30] 3
[2017/10/01 17:19:30] 4
[2017/10/01 17:19:30] 5
[2017/10/01 17:19:30] 6
[2017/10/01 17:19:32] 0
[2017/10/01 17:19:32] 1
[2017/10/01 17:19:32] 2
[2017/10/01 17:19:32] 3
[2017/10/01 17:19:32] 4
[2017/10/01 17:19:32] 5
[2017/10/01 17:19:32] 6
[2017/10/01 17:19:34] remove 0 || Prev: 1000
[2017/10/01 17:19:34] 0
[2017/10/01 17:19:34] 0
[2017/10/01 17:19:34] 0
[2017/10/01 17:19:34] 0
[2017/10/01 17:19:34] 0
how could be the next id the array's max size?

Код HTML:
	foreach(new serveritemid : ServerItems)
	{
		count[serveritemid]++;
		if(count[serveritemid] == 3)
		{
			new next = Iter_Next(ServerItems, serveritemid);
			printf("remove %d || next: %d", serveritemid, next);
			Iter_SafeRemove(ServerItems, serveritemid, next);
		}
		if(!Iter_Count(ServerItems))
		{
			printf("ServerItems empty");
		}
		printf("%d", serveritemid);
	}
output

Код HTML:
[2017/10/01 17:17:15] 0
[2017/10/01 17:17:15] 1
[2017/10/01 17:17:15] 2
[2017/10/01 17:17:15] 3
[2017/10/01 17:17:15] 4
[2017/10/01 17:17:15] 5
[2017/10/01 17:17:15] 6
[2017/10/01 17:17:17] 0
[2017/10/01 17:17:17] 1
[2017/10/01 17:17:17] 2
[2017/10/01 17:17:17] 3
[2017/10/01 17:17:17] 4
[2017/10/01 17:17:17] 5
[2017/10/01 17:17:17] 6
[2017/10/01 17:17:19] remove 0 || next: 1
[2017/10/01 17:17:19] 0
[2017/10/01 17:17:19] 0
[2017/10/01 17:17:19] 0

thanks for your help
Reply
#2

anyone?:/
Reply
#3

Just stop using Iter_Next and Iter_Prev.

new next;
Iter_SafeRemove(Serveritems, serveritemid, next);
serveritemid = next;
Reply
#4

I also tried that.

Код:
	foreach(new serveritemid : ServerItems)
	{
		count[serveritemid]++;
		if(count[serveritemid] == 3)
		{
			new next;
			Iter_SafeRemove(ServerItems, serveritemid, next);
			serveritemid = next;
		}
		if(!Iter_Count(ServerItems))
		{
			printf("ServerItems empty");
		}
		printf("%d", serveritemid);
	}
Код HTML:
[2017/10/01 19:31:32] 0
[2017/10/01 19:31:32] 1
[2017/10/01 19:31:32] 2
[2017/10/01 19:31:32] 3
[2017/10/01 19:31:32] 4
[2017/10/01 19:31:32] 5
[2017/10/01 19:31:32] 6
[2017/10/01 19:31:34] 0
[2017/10/01 19:31:34] 1
[2017/10/01 19:31:34] 2
[2017/10/01 19:31:34] 3
[2017/10/01 19:31:34] 4
[2017/10/01 19:31:34] 5
[2017/10/01 19:31:34] 6
[2017/10/01 19:31:36] 1000
[2017/10/01 19:31:36] 1000
[2017/10/01 19:31:36] 0
[2017/10/01 19:31:36] 1
[2017/10/01 19:31:36] 2
[2017/10/01 19:31:36] 3
[2017/10/01 19:31:36] ServerItems empty
[2017/10/01 19:31:36] 4
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 2
[2017/10/01 19:31:36] ServerItems empty
[2017/10/01 19:31:36] 4
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 2
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
[2017/10/01 19:31:38] ServerItems empty
[2017/10/01 19:31:38] 5
Reply
#5

This problem is annoying, I made a workaround that consists of adding the IDs I want to remove from an iterator to an array and once the foreach finishes I run the array (in a for() loop) and remove them normally.

Probably not the best way, but fixed the issue.
Reply
#6

Yeah i know. I could solve in that way, but i dont want to use that method, that's why i'm using foreach. Any advices? :/
Reply
#7

Quote:
Originally Posted by m4karow
Посмотреть сообщение
Yeah i know. I could solve in that way, but i dont want to use that method, that's why i'm using foreach. Any advices? :/
Tell me if it's working, i have something like this in my GM and work without any crash/problem.

Код:
foreach(new serveritemid : ServerItems)
	{
		count[serveritemid]++;
		if(count[serveritemid] == 3)
		{
			new next = serveritemid ;
			Iter_SafeRemove(ServerItems, next, serveritemid );
			serveritemid = next;
		}
		if(!Iter_Count(ServerItems))
		{
			printf("ServerItems empty");
		}
		printf("%d", serveritemid);
	}
Reply
#8

not works
Reply
#9

Quote:
Originally Posted by m4karow
Посмотреть сообщение
not works
hmm. try delete serveritemid = next; from the code.
Reply
#10

Quote:
Originally Posted by None1337
Посмотреть сообщение
hmm. try delete serveritemid = next; from the code.
Same problem.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)