//GetRouteFromTo(...);
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[0]: 47
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[1]: 103
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[2]: 101
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[3]: 116
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[4]: 97
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[5]: 114
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[6]: 114
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[7]: 97
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[8]: 121
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[9]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[10]: 1
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[11]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[12]: 101
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[13]: 116
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[14]: 97
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[15]: 114
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[16]: 114
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[17]: 97
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[18]: 121
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[19]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[20]: 71
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[21]: 101
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[22]: 116
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[23]: 97
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[24]: 114
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[25]: 114
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[26]: 97
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[27]: 121
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[28]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[29]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[30]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[31]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[32]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[33]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[34]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[35]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[36]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[37]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[38]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[39]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[40]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[41]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[42]: 0
[21:49:13] [System CMD: getarray] - Getting Route Array - node_id_array[43]: 0
//see "static cell AMX_NATIVE_CALL n_StoreArray( AMX* amx, cell* params )"
struct RouteData
{
cell * Paths;
int amount_of_nodes;
int id;
RouteData(cell *Paths_,int amount_of_nodes_, int id_)
{
Paths = NULL;
amount_of_nodes = NULL;
id = NULL;
Paths = Paths_;
amount_of_nodes = amount_of_nodes_;
id = id_;
}
};
vector <RouteData> RouteVector;
vector <int> RouteID;
static cell AMX_NATIVE_CALL n_StoreArray( AMX* amx, cell* params )
{
int IDcounter = 0;
for(int i = 0,j = RouteID.size(); i < j; ++i)
{
if(RouteID.at(i) == IDcounter)
{
i = 0;
IDcounter++;
}
}
cell *dest = NULL;
amx_GetAddr(amx,params[2],&dest);
int amount_of_nodes = params[1];
RouteID.push_back(IDcounter);
RouteVector.push_back(RouteData(dest,amount_of_nodes,IDcounter));
//Tried this to fix it:
for(int i = 0, j = amount_of_nodes; i < j; ++i)//doesn't fix it? :S <--- this
{
cout << i << "|" << RouteVector.back().Paths[i] << "|" << dest[i] << "\r\n";
RouteVector.back().Paths[i] = dest[i];
cout << i << "|" << RouteVector.back().Paths[i] << "|" << dest[i] << "\r\n";
}
//
return IDcounter;
}
static cell AMX_NATIVE_CALL n_ReturnArray( AMX* amx, cell* params )
{
for(int i = 0, j = RouteVector.size(); i < j; ++i)
{
if(RouteVector.at(i).id == params[1])
{
cell *dest = NULL;
amx_GetAddr(amx,params[2],&dest);
cout << "n_ReturnArray --> params[1] == " << params[1] << " params[2] == " << params[2] << " params[3] == " << params[3] << " AMX Addr: " << dest << "|" << *dest << "\r\n";
for(int a = 0, b = params[3]; a < b; ++a)
{
cout << RouteVector.at(i).Paths[a] << "\r\n";
dest[a] = RouteVector.at(i).Paths[a];
}
return RouteVector.at(i).amount_of_nodes;
}
}
return 0;
}
static cell AMX_NATIVE_CALL n_GetRouteAtPos( AMX* amx, cell* params )
{
for(int i = 0, j = RouteVector.size(); i < j; ++i)
{
if(RouteVector.at(i).id == params[1])
{
if(RouteVector.at(i).amount_of_nodes > i)
{
cell *dest = NULL;
amx_GetAddr(amx,params[3],&dest);
cout << "n_GetRouteAtPos --> params[1] == " << params[1] << " params[2] == " << params[2] << " params[3] == " << params[3] << " AMX Addr: " << dest << "|" << *dest << "\r\n";
*dest = RouteVector.at(i).amount_of_nodes;
cout << "n_GetRouteAtPos(2) --> AMX Addr: " << dest << "|" << *dest << "\r\n";
return RouteVector.at(i).Paths[params[2]];
}
}
}
return 0;
}
static cell AMX_NATIVE_CALL n_GetRouteFromTo( AMX* amx, cell* params )
{
for(int i = 0, j = RouteVector.size(); i < j; ++i)
{
if(RouteVector.at(i).id == params[1])
{
cell *dest = NULL;
amx_GetAddr(amx,params[4],&dest);
cout << "n_GetRouteFromTo --> params[1] == " << params[1] << " params[2] == " << params[2] << " params[3] == " << params[3] << " params[4] == " << params[4] << " AMX Addr: " << dest << "|" << *dest << "\r\n";
int c = -1;
for(int a = params[2], b = params[3]; a < b; ++a)
{
c++;
if(c == params[5])
{
return c;
}
dest[c] = RouteVector.at(i).Paths[a];
cout << a << "|" << dest[c] << "|" << RouteVector.at(i).Paths[a] << "\r\n";
}
return c;
}
}
return 0;
}
It also changes when you alt-tab and unpause! don't forget that one (yes, I know it's pretty weird)
|
cell *dest = NULL; amx_GetAddr(amx,params[2],&dest); int amount_of_nodes = params[1]; RouteID.push_back(IDcounter); RouteVector.push_back(RouteData(dest,amount_of_nodes,IDcounter));
Code:
cell *dest = NULL; amx_GetAddr(amx,params[2],&dest); int amount_of_nodes = params[1]; RouteID.push_back(IDcounter); RouteVector.push_back(RouteData(dest,amount_of_nodes,IDcounter)); |
struct RouteData
{
cell * Paths;
int amount_of_nodes;
int id;
RouteData(cell *Paths_,int amount_of_nodes_, int id_)
{
memcpy(Paths,Paths_,amount_of_nodes);// <- This crashes the server
//edit: this too doesn't work:
memcpy(&Paths,Paths_,(amount_of_nodes-1));
//end of edit
//Paths = Paths_;
amount_of_nodes = amount_of_nodes_;
id = id_;
}
};
Paths = (cell*)malloc(amount_of_nodes_); memcpy(Paths, Paths_, amount_of_nodes);
free(RouteVector[x].Paths);
Well, if all above element 43 aren't changed, then this is because that memory is not used after it was freed, because you don't allocate that much memory again. You can try adding a bigger array (in PAWN) in another function (which is called anywhere after the function where you call StoreArray has returned) to see if then more values are changed (you should of course remove them again after that).
paths = dest only sets the pointer of "paths", so then "paths" would point to the same location as "dest". Before using memcpy you first need to allocate some memory to write to: Code:
Paths = (cell*)malloc(amount_of_nodes_); memcpy(Paths, Paths_, amount_of_nodes); Code:
free(RouteVector[x].Paths); |
Console input: reloadfs c
[19:12:49] Filter script 'c.amx' unloaded.
[19:12:49] Filterscript 'c.amx' loaded.
[19:12:49] outc()--begin--
[19:12:49] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[19:12:49] ID:0
[19:12:49] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[19:12:49] GetRouteArray(id,goodarr)::25
[19:12:49] goodarr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[19:12:49] GetRouteArray(id,minarr)::25
[19:12:49] minarr: 0 1 2 3 4 5 6 7 8 9
[19:12:49] GetRouteArray(id,secarr)::25
[19:12:49] secarr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 0 0 1 2 3
[19:12:49] ArrSize: 25
[19:12:49] GetRouteArrayFromTo(id,0,25,fromtoarr)::24
[19:12:49] fromtoarr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[19:12:49] GetRouteArrayFromTo(id,10,22,frx)::11
[19:12:49] frx: 10 11 12 13 14 15 16 17 18 19 20 21
[19:12:49] outc()--end--//Timer set...
[19:13:00] delayed()--begin--
[19:13:00] ID:0
[19:13:00] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[19:13:00] GetRouteArray(id,goodarr)::25
[19:13:00] goodarr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[19:13:00] GetRouteArray(id,minarr)::25
[19:13:00] minarr: 0 1 2 3 4 5 6 7 8 9
[19:13:00] GetRouteArray(id,secarr)::25
[19:13:00] secarr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 0 0 1 2 3
[19:13:00] ArrSize: 25
[19:13:00] GetRouteArrayFromTo(id,0,25,fromtoarr)::24
[19:13:00] fromtoarr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[19:13:00] GetRouteArrayFromTo(id,10,22,frx)::11
[19:13:00] frx: 10 11 12 13 14 15 16 17 18 19 20 21
[19:13:00] Delete:1
[19:13:00] delayed()--end--
#include <a_samp>
#include <routeConnector>
public OnFilterScriptInit()
{
SetTimer("outc",0,0);
return 1;
}
/*Testing this functions:
GetRouteArray(ID,destination[],size = sizeof(destination));
GetRouteAtPos(ID,Array_Pos,&amount_of_nodes=0);
StoreRouteArray(amount_of_nodes,array[]);
DeleteArray(ID);
GetRouteArrayFromTo(ID,From,To,dest[],size=sizeof(dest));
GetRouteArraySize(ID);
*/
new string[256];
new array[25] =
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
new id;
new secarr[30];
new minarr[10];
new goodarr[25];
new fromtoarr[25];
new frx[12];
forward outc();
public outc()
{
print("outc()--begin--");
string[0] = 0;
for(new i = 0; i < 25; ++i)
{
format(string,256,"%s %d",string,array[i]);
}
print(string);
id = StoreRouteArray(sizeof(array),array);
printf(" ID:%d",id);
string[0] = 0;
for(new i = 0; i < 25; ++i)
{
format(string,256,"%s %d",string,GetRouteAtPos(id,i));
}
print(string);
//
printf("GetRouteArray(id,goodarr)::%d",GetRouteArray(id,goodarr));
string[0] = 0;
for(new i = 0; i < sizeof(goodarr); ++i)
{
format(string,256,"%s %d",string,goodarr[i]);
}
printf(" goodarr: %s",string);
//
printf("GetRouteArray(id,minarr)::%d",GetRouteArray(id,minarr));
string[0] = 0;
for(new i = 0; i < sizeof(minarr); ++i)
{
format(string,256,"%s %d",string,minarr[i]);
}
printf(" minarr: %s",string);
//
printf("GetRouteArray(id,secarr)::%d",GetRouteArray(id,secarr));
string[0] = 0;
for(new i = 0; i < sizeof(secarr); ++i)
{
format(string,256,"%s %d",string,secarr[i]);
}
printf(" secarr: %s",string);
//
printf(" ArrSize: %d",GetRouteArraySize(id));
//
printf("GetRouteArrayFromTo(id,0,25,fromtoarr)::%d",GetRouteArrayFromTo(id,0,25,fromtoarr));
string[0] = 0;
for(new i = 0; i < sizeof(fromtoarr); ++i)
{
format(string,256,"%s %d",string,fromtoarr[i]);
}
printf(" fromtoarr: %s",string);
//
printf("GetRouteArrayFromTo(id,10,22,frx)::%d",GetRouteArrayFromTo(id,10,22,frx));
string[0] = 0;
for(new i = 0; i < sizeof(frx); ++i)
{
format(string,256,"%s %d",string,frx[i]);
}
printf(" frx: %s",string);
//
SetTimer("delayed",5000,0);
print("outc()--end--");
return 1;
}
forward delayed();
public delayed()
{
print("delayed()--begin--");
printf(" ID:%d",id);
string[0] = 0;
for(new i = 0; i < 25; ++i)
{
format(string,256,"%s %d",string,GetRouteAtPos(id,i));
}
print(string);
//
printf("GetRouteArray(id,goodarr)::%d",GetRouteArray(id,goodarr));
string[0] = 0;
for(new i = 0; i < sizeof(goodarr); ++i)
{
format(string,256,"%s %d",string,goodarr[i]);
}
printf(" goodarr: %s",string);
//
printf("GetRouteArray(id,minarr)::%d",GetRouteArray(id,minarr));
string[0] = 0;
for(new i = 0; i < sizeof(minarr); ++i)
{
format(string,256,"%s %d",string,minarr[i]);
}
printf(" minarr: %s",string);
//
printf("GetRouteArray(id,secarr)::%d",GetRouteArray(id,secarr));
string[0] = 0;
for(new i = 0; i < sizeof(secarr); ++i)
{
format(string,256,"%s %d",string,secarr[i]);
}
printf(" secarr: %s",string);
//
printf(" ArrSize: %d",GetRouteArraySize(id));
//
printf("GetRouteArrayFromTo(id,0,25,fromtoarr)::%d",GetRouteArrayFromTo(id,0,25,fromtoarr));
string[0] = 0;
for(new i = 0; i < sizeof(fromtoarr); ++i)
{
format(string,256,"%s %d",string,fromtoarr[i]);
}
printf(" fromtoarr: %s",string);
//
printf("GetRouteArrayFromTo(id,10,22,frx)::%d",GetRouteArrayFromTo(id,10,22,frx));
string[0] = 0;
for(new i = 0; i < sizeof(frx); ++i)
{
format(string,256,"%s %d",string,frx[i]);
}
printf(" frx: %s",string);
//
printf(" Delete:%d",DeleteArray(id));
print("delayed()--end--");
return 1;
}
//your code...
//thanks to fabsch for helping fixing the array bug
struct RouteData
{
cell * Paths;
int amount_of_nodes;
int id;
RouteData(cell *Paths_,int amount_of_nodes_, int id_)
{
Paths = (cell*)malloc(amount_of_nodes_);
memcpy(Paths, Paths_, amount_of_nodes_);
//Paths = Paths_;
amount_of_nodes = amount_of_nodes_;
id = id_;
}
};
//
static cell AMX_NATIVE_CALL n_DeleteArray( AMX* amx, cell* params )
{
for(int i = 0, j = RouteVector.size(); i < j; ++i)
{
if(RouteVector.at(i).id == params[1])
{
RouteID.erase(RouteID.begin()+i);
free(RouteVector[i].Paths);
RouteVector.erase(RouteVector.begin()+i);
return 1;
}
}
return 0;
}
[17:25:06] outc()--begin--
[17:25:06] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[17:25:06] ID:0
[17:25:06] 0 1 2 3 4 5 6 0 648060458 -- 22 0 0 0 0 0 0 0 648060471 -- 27 0 0 0 0
[17:25:06] GetRouteArray(id,goodarr)::25
[17:25:06] goodarr: 0 1 2 3 4 5 6 0 648060458 -- 22 0 0 0 0 0 0 0 648060471 -- 27 0 0 0 0
[17:25:06] GetRouteArray(id,minarr)::25
[17:25:06] minarr: 0 1 2 3 4 5 6 0 648060458 --
[17:25:06] GetRouteArray(id,secarr)::25
[17:25:06] secarr: 0 1 2 3 4 5 6 0 648060458 -- 22 0 0 0 0 0 0 0 648060471 -- 27 0 0 0 0 0 0 0 648060464 --
[17:25:06] ArrSize: 25
[17:25:06] GetRouteArrayFromTo(id,0,25,fromtoarr)::24
[17:25:06] fromtoarr: 0 1 2 3 4 5 6 0 648060458 -- 22 0 0 0 0 0 0 0 648060471 -- 27 0 0 0 0
[17:25:06] GetRouteArrayFromTo(id,10,22,frx)::11
[17:25:06] frx: 22 0 0 0 0 0 0 0 648060471 -- 27 0
[17:25:06] outc()--end--
[17:25:33] delayed()--begin--
[17:25:33] ID:0
[17:25:33] 0 1 2 3 4 5 6 0 648060458 -- 1701969942 1667584869 1415868015 1919249769 1862300672 1684368754 0 0 648060471 -- 27 0 0 0 0
[17:25:33] GetRouteArray(id,goodarr)::25
[17:25:33] goodarr: 0 1 2 3 4 5 6 0 648060458 -- 1701969942 1667584869 1415868015 1919249769 1862300672 1684368754 0 0 648060471 -- 27 0 0 0 0
[17:25:33] GetRouteArray(id,minarr)::25
[17:25:33] minarr: 0 1 2 3 4 5 6 0 648060458 --
[17:25:33] GetRouteArray(id,secarr)::25
[17:25:33] secarr: 0 1 2 3 4 5 6 0 648060458 -- 1701969942 1667584869 1415868015 1919249769 1862300672 1684368754 0 0 648060471 -- 27 0 0 0 0 0 0 0 648060464 --
[17:25:33] ArrSize: 25
[17:25:33] GetRouteArrayFromTo(id,0,25,fromtoarr)::24
[17:25:33] fromtoarr: 0 1 2 3 4 5 6 0 648060458 -- 1701969942 1667584869 1415868015 1919249769 1862300672 1684368754 0 0 648060471 -- 27 0 0 0 0
[17:25:33] GetRouteArrayFromTo(id,10,22,frx)::11
[17:25:33] frx: 1701969942 1667584869 1415868015 1919249769 1862300672 1684368754 0 0 648060471 -- 27 0
[17:25:33] Delete:1
[17:25:33] delayed()--end--
Paths = (cell*)malloc(amount_of_nodes_ * sizeof(cell)); memcpy(Paths, Paths_, amount_of_nodes_ * sizeof(cell));