I am developing a little application that first adds some values to an array of pointers defined as a global variable:
#define MAX_LOCATIONS 32
LocationDTO* locations[MAX_LOCATIONS];
int nLocations = 0;
I use the following method to add the LocationDTO object references to the array:
bool addLocation(Location *location, string name){
LocationDTO l(name, location);
if(nLocations < MAX_LOCATIONS){
locations[nLocations] = &l;
nLocations++;
return true;
}else{
return false;
}
}
And I call this function from the main method this way:
Location l1(1,2);
addLocation(&l1, "one");
Location l2(2,3);
addLocation(&l2, "two");
Location l3(4,5);
addLocation(&l3, "three");
After all values are added I start a thread that will process this array. Thread definition:
void* server_thread(void* args){
// Some code
int i;
for(i=0; i < nLocations; i++){
LocationDTO* l = locations[i];
// More Code
}
}
The problem is that, at this point in the thread, the objects contained inside locations don't have anymore the values I assigned to them.
Does this problem happen because I am creating the objects inside addLocation and then saving the reference in the array?