Hi, thanks everyone who has helped out on this. The issue is resolved. Turns out to be indeed due to a bug in the code on handling memory allocations. The issue was with the following, where I have allocated memory without including the C String "\0" trailing character. For example, if the string is "I have", I was only doing "numOfFields = 6" rather than "numOfFields = 7".
Code:
int * storage = (int*) malloc(numOfFields * sizeof(int));
Also, while using Valgrind, it was reporting that the code is leaking memory. When I added some "free()" it introduced another bug. The reason is better explained with the following psuedo code.
Code:
std::map<std::string, int *>map_ObjName_PointerAddress;
std::map<std::string, int *>map_ObjName_FieldArrayAddress;
void retrieveObjectsNamesAndPointers() {
int * address;
// Some code to assign address;
// Do something here to insert mapping between "object names" and "pointers to these objects" to map_ObjName_PointerAddress.
map_ObjName_PointerAddress.insert(std::pair<std::string, int *>("ObjName1", address));
}
void doSomethingWithObject(ObjectType * pObject) {
// Call method lookUpFields() to retrieve some details about the objects before doing something with them.
lookUpFields(pObject);
// Routine to do something here based on the fields retrieved for the objects.
}
void lookUpFields(ObjectType * pObject) {
int * fieldsArray = (int*) malloc(numOfFields * sizeof(int));
fieldsArray = CObject::map_ObjName_PointerAddress.find(pObject->getName)->second;
// Insert the new object name to fields array address map to map_ObjName_FieldArrayAddress
map_ObjName_FieldArrayAddress.insert(std::pair<std::string, int *>(pObject->getName, fieldsArray));
// Can not do following upon exit:
// free(fieldsArray);
// fieldsArray = 0;
}
The code will invoke the following functions in sequence.
retrieveObjectsNamesAndPointers()
doSomethingWithObject()
Therefore, following sequence of events occur:
1) Memory allocated in the function retrieveObjectsNamesAndPointers().
2) Pointer to this memory is inserted into map_ObjName_PointerAddress.
3) Pointer to this memory, as stored in map_ObjName_PointerAddress, is retrieved in lookUpFields(). This memory address retrieved is then assigned to the local pointer fieldsArray.
4) When the "free(fieldsArray)" is called (highlighted in red and commented out here), it actually frees the memory originally allocated in step 1. Therefore, when we exits lookUpFields() and returns to doSomethingWithObject(), the items stored in the memory location is no longer available, causing problems.
Also the following line is not serving any purpose.
Code:
int * fieldsArray = (int*) malloc(numOfFields * sizeof(int));
This is because the following line, is not copying the entire block of memory to the new memory location. We're merely allocating memory on the heap (so when lookUpFields() exits this fieldsArray is still there which is also why Valgrind is complaining about leaking memory), and setting the first memory location of fieldsArray to point to the memory location assigned in Step 1.
Code:
fieldsArray = CObject::map_ObjName_PointerAddress.find(pObject->getName)->second;
In the end, I simply removed the lines highlighted in both red and blue. I simply do the following instead.
Code:
map_ObjName_FieldArrayAddress.insert(std::pair<std::string, int *>(pObject->getName, CObject::map_ObjName_PointerAddress.find(pObject->getName)->second));