SA-MP Forums Archive
Foreach saferemove causes infinite loop - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Foreach saferemove causes infinite loop (/showthread.php?tid=642491)



Foreach saferemove causes infinite loop - m4karow - 01.10.2017

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


Re: Foreach saferemove causes infinite loop - m4karow - 01.10.2017

anyone?:/


Re: Foreach saferemove causes infinite loop - raydx - 01.10.2017

Just stop using Iter_Next and Iter_Prev.

new next;
Iter_SafeRemove(Serveritems, serveritemid, next);
serveritemid = next;


Re: Foreach saferemove causes infinite loop - m4karow - 01.10.2017

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



Re: Foreach saferemove causes infinite loop - Marricio - 01.10.2017

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.


Re: Foreach saferemove causes infinite loop - m4karow - 01.10.2017

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? :/


Re: Foreach saferemove causes infinite loop - None1337 - 01.10.2017

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);
	}



Re: Foreach saferemove causes infinite loop - m4karow - 01.10.2017

not works


Re: Foreach saferemove causes infinite loop - None1337 - 01.10.2017

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


Re: Foreach saferemove causes infinite loop - m4karow - 01.10.2017

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