SA-MP Forums Archive
Foreach crashing the server, any idea why? - 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 crashing the server, any idea why? (/showthread.php?tid=637842)



Foreach crashing the server, any idea why? - Sjn - 22.07.2017

So, I have an iterator to hold the server vehicles;

PHP код:
new Iterator:Vehicles<MAX_VEHICLES>;
vehicle_Create(modelFloat:xFloat:yFloat:zFloat:ac1c2delay)
{
    new 
vehicleid CreateVehicle(modelxyzac1c2delay);
    
    
Iter_Add(Vehiclesvehicleid);
    
    
printf("Added vehicleid %i to the iterator."vehicleid);
    
Vehicle(vehicleidModel) = model;
    
Vehicle(vehicleidFuel) = vehicle_ReturnFuelTank(vehicleid);
    
Vehicle(vehicleidColor1) = c1;
    
Vehicle(vehicleidColor2) = c2;
    return 
vehicleid;
}
vehicle_Destroy(vehicleid)
{
    
Iter_Remove(Vehiclesvehicleid);
    
DestroyVehicle(vehicleid);
    
printf("Removed vehicleid %i from the iterator."vehicleid);
}
#if defined _ALS_DestroyVehicle
    #undef DestroyVehicle
#else
    #define _ALS_DestroyVehicle
#endif
#define DestroyVehicle vehicle_Destroy 
This works, however, when I call the function to destroy the vehicle inside a loop like;

PHP код:
foreach(new Vehicles)
{
    
// Few if statements on this line to check for specific vehicles to delete.
    
{
        
DestroyVehicle(i);
    }

It's crashing the server. When I use DestroyVehicle for a specific vehicle id, it works fine tho. The printf inside the DestroyVehicle function does not print out anything when it crashes the server nor there are any crash log, server just freezes at it is.

Can anyone help me point out the issue? I am using the standalone version of foraech.


Re: Foreach crashing the server, any idea why? - Eoussama - 22.07.2017

Because you're removing an iterator while in a foreach loop, use Iter_SafeRemove instead


Re: Foreach crashing the server, any idea why? - Paulice - 22.07.2017

https://sampforum.blast.hk/showthread.php?tid=637444


Re: Foreach crashing the server, any idea why? - Dayrion - 22.07.2017

There is already an existant iterator for vehicle like players