I encountered a problem when debugging my model (written in C++) in Eclipse CDT. The problem is that when I pass a structure variable, who contains various member variables such as string or vector, to a function by reference, the value of certain member variables are not updated in the scope of that function. More details are provided as below:
struct ModelConfig {
//... here are some other variables and constructors
vector<int> crop_list;
string path_to_input;
//....
};
Say now I start debugging in GDB, and here is the first function call :
void modelMain::setupModel( const ModelConfig & sim_setting ){
//... some operations to configure the model using 'sim_setting'
/* 1.3 - Initialize the land */
Set_Environment(k_farm_land, sim_setting);
// breakpoint here, printing out the value of 'sim_etting' shows 'sim_setting.path_to_input = "data/"' ; Then I enter into 'Set_Environment' function ...
//...
}
void Set_Environment(vector<Soil> & farm_land, const ModelConfig & sim_setting) {
int EXP_ID = sim_setting.EXP_ID;
string strTmp_a;
strTmp_a = sim_setting.path_to_input + "soil/parameters.txt"; // Problem is here: the GDB shows here that sim_setting.path_to_input = " ". I am expecting strTmp_a = "data/soil/parameters.txt" which now is "soil/parameters.txt" ;
//... operations for reading data
}
The sim_setting.path_to_input variable should hold the string value named data/, which is correct during the call in setupModel(...), but the value is lost (or the address is changed actually) during the call in Set_Environment(...)...
When using the GDB debug in Eclipse to trace the address of the variables, I notice that the address of sim_setting seems correct in both setupModel and Set_Environment, but the member variable of path_to_input and crop_list changed into other place, which cause the lost of data. The value of crop_list is created using .push_back().
I did not get the point since I am passing the variable by reference. The only thing that I can imagine is due to the value assignment of string and vector. Anyone have theory for this ? Thank you very much in advance !
Related
I'm developing in C++. I have 2 objects : which are the StandaloneAgent and the ConfigManager.
I want from the Standalone Agent to reach a char * variable in the ConfigManager with a function .
int32_t StandaloneAgent::getConfigFile(char * file){
int32_t code = mConfigMgr->getConfigFile(file);
return code;
}
int32_t ConfigManager::getConfigFile(char * file){
file = mConfigFile;
return 0;
}
Here, the ConfigManager objet is an attribute of the StandaloneAgent with the
ConfigManager * mConfigMgr; variable.
And the file I want to reach is located in the mConfigFile attribute of the ConfigManager.
The problem is :
While putting std::cout,
the mConfigFile in the is OK and
the file variable after the
file = mConfigFile;
is OK
But when I return in the getConfigFile() function of the standalone agent, the file variable is a null pointer and I don't know why.
Is there a C++ specification that I am missing ?
Arguments to functions are either passed by-value or by-reference. Any variable passed by-value will be local to the function and any changes made to it is local to the function too. This means that when to assign a new value to the pointer (file = mConfigFile) you only change the pointer in the function. The pointer you passed as an argument is unaffected.
When taking arguments by-reference, the variable inside the function references the variable used to call the function. Any changes made to that variable will be directly made to the variable used in the call to the function.
In your case, that means that you need to change the ConfigManager::getConfigFile function:
int32_t ConfigManager::getConfigFile(char*& file){
file = mConfigFile; // the change is made to the variable used as an argument
return 0;
}
If the pointer passed to StandaloneAgent::getConfigFile is also supposed to be changed (I assume it is), you need the same change there:
int32_t StandaloneAgent::getConfigFile(char*& file){
int32_t code = mConfigMgr->getConfigFile(file);
return code;
}
I've a problem in my program. I'm getting a exception that crashes the program. Firstly, i innitialize my struct object in my class header. Like so:
struct Settings {
DWORD AccesibilityMode;
bool Raw;
DWORD Type;
};
Settings * thread_settings = new Settings(); //Here im getting the pointer of the newly created
object
Now i've innitialized what i need. Going on, now i need to set the variables of the struct pointer to the specific settings that i want (The struct is going to be used as a option/settings thingy). The struct pointer is private so i wrote a getter-method that returns the address of the pointer. It's written like this (in my .cpp file of the class):
Client::Settings* Client::Thread_Settings() {
return thread_settings;
}
Now, the method is a pointer that is accessible everywhere. I'm creating a object of the class and calling the address. Then im setting the vars like so (still no error to this point).
(object already defined)
client.Thread_Settings()->AcessibilityMode = 1; //Normally i have macros that are setted to these values
client.Thread_Settings()->Raw = false;
client.Thread_Settings()->Type = 33;
Lastly, the main function executes a method from the same class as the object struct pointer. These methods have if-statements that are checking the defined variables in the struct. These if lines are written like this:
if (thread_settings->Recv_BufferAccesibilityMode == 1 /*normally instead for 1 i've a macro*/ ) {
...... //rest of code
}
Now, i've got an access violation error that reads some unwritten memory (checked in an assembler) sometimes on 0x11C and sometimes at 0x12C. The error in vs19 looks loke this:
Exception thrown: read access violation.
this->thread_settings was 0x11C.
Do anyone have any thoughts? Appreciate any help :D
I am trying to replicate a big C++ library. It has the following structure of code
RobotRunner.h
class RobotRunner
{
public:
VectorNavData* vectorNavData;
};
RobotRunner.cpp
void RobotRunner::run()
{
printf("Quaternion[0]: %f \n", vectorNavData->quat[0]); //Output: 243235653487854 - Abnormal values
}
SimulationBridge.h
class SimulationBridge
{
private:
VectorNavData _vectorNavData; // Actual value of vectornavdata from the robot is stored here
RobotRunner* _robotRunner = nullptr; //Pointer to the RobotRunner Object
}
SimulationBridge.cpp
void SimulationBridge::init()
{
_robotRunner = new RobotRunner();
printf("Quaternion[0]: %f \n", _vectorNavData.quat[0]); // Output: 0.43 - Normal and expected
_robotRunner->vectorNavData = &_vectorNavData;
}
void SimulationBridge::run()
{
_robotRunner->run();
}
//This function runs continuously and updates the _vectorNavData in a separate thread
void SimulationBridge::readIMU()
{
while(true)
{
//_lowState stores the values of different robot parameters at a given time
_vectorNavData.accelerometer[0] = _lowState.imu.accelerometer[0];
_vectorNavData.accelerometer[1] = _lowState.imu.accelerometer[1];
_vectorNavData.accelerometer[2] = _lowState.imu.accelerometer[2];
_vectorNavData.quat[0] = _lowState.imu.quaternion[1];
_vectorNavData.quat[1] = _lowState.imu.quaternion[2];
_vectorNavData.quat[2] = _lowState.imu.quaternion[3];
_vectorNavData.quat[3] = _lowState.imu.quaternion[0];
_vectorNavData.gyro[0] = _lowState.imu.gyroscope[0];
_vectorNavData.gyro[1] = _lowState.imu.gyroscope[1];
_vectorNavData.gyro[2] = _lowState.imu.gyroscope[2];
}
}
VectorNavData is a struct which stores the details about the orientation of the robot. It has the following definition
struct VectorNavData {
Vec3<float> accelerometer;
Vec3<float> gyro;
Quat<float> quat;
};
I have included only the necessary part of the code here for brevity.
Code Explanation:
SimulationBridge class communicates with the robot in the simulation. It takes in vectorNavData and stores it in the member variable _vectorNavData. SimulationBridge also contains the pointer to the RobotRunner class as one of it's member. I am allocating the address of _vectorNavData object to the pointer _robotRunner->vectorNavData (check SimulationBridge.cpp). Inside the RobotRunner class I deference this pointer and use the values in other parts of the code.
Problem:
If I print the vectorNavData inside the SimulationBridge.cpp the values seems to be normal. But after assigning the pointer of the same object to the robot runner, if I print the values there the values seems to be abnormally high. My question is, is this way of using pointers for dynamic allocation recommended? If not what is the best alternative way I can use?
Another important point to note is, I am compiling the code with CMake and "-O3" optimization flag is set to the CMAKE_CXX_FLAGS. If I remove this flag, the code sorta works fine for the above object pointer but I am still getting similar error for another object pointer in another part of the code. I have not included that here because it's pretty complex to describe the code structure and the problem essentially is the same.
I've defined in my foo.h the following variables
#define APILONG long
#define APIDOUBLE double
#define APISTRING const char*
And also the following struct
struct SetupData
{
APISTRING customerName;
APIDOUBLE quantity;
APILONG startDate;
};
Now, in my foo.cpp i have the following method where I need to assign values to the members of the struct that are being pulled from a .config file.
APILONG doSomething(APISTRING* setupOrder, APILONG* setupActive, struct SetupData** setupData)
{
//load config file
Config config("rommel.config");
//assign a string value to APISTRING* (No issue here.)
*setupOrder= config.pString("setupOrder").c_str();
//assign a value (No issue here, atleast not that I know of..)
*setupActive = config.pDouble("setupActive");
//assign values to members of struct**
(*setupData)->customerName = config.pString("setupDataCustomerName").c_str();
(*setupData)->quantity = config.pDouble("setupDataQuantity");
(*setupData)->startDate = config.pDouble("setupDataStartDate");
//do other stuff..
}
When I compile and rebuild it does not give me any error messages. But when I try to run the actual program it crashes. (Am using Dev-C++, Visual Studio was causing problems..)
I do however get a chance to see the values assigned before it crashes and it looks like the values are not being assigned (Null or strange characters).
I've tried variations of the
(*setupData)->startDate ..
line and also I have tried declaring the struct in the method like the following, but to no avail.
struct SetupData stpData;
*setupData = &stpData;
Any help would be greatly appreciated. I previously posted another question which is related to this, it contains some pretty useful information aswell. I'll leave the link in case it helps. "C++ Set value for a char**"
The config object is local to the doSomething function.
The setupOrder pointer points to what appears to be a
member of the config object.
The config object will go out of scope at the end of the doSomething function and can be de-allocated at that point.
At that point, what the setupOrder pointer "points" to
might no longer be valid.
It is a good idea to make sure that after the call to doSomething ends,
that all the pointers still point to objects that still exist.
You may have already done this, but I just wanted to check.
I'm having a seg fault: 11 when I run a particular program. I feel like this problem wasn't present before I upgraded my system to Mac OS X 10.9, but it's possible I just overlooked it..
Anyway, my function looks like:
// this applies a warp to the field given, and saves output. simple!
void Apply(string warpName, string fieldName, bool conserve, string outName) {
// get lon, lat dimensions of warp
int noLongs = GetDimension(warpName, 3, "warp");
int noLats = GetDimension(warpName, 2, "warp");
int origNoLongs = noLongs, origNoLats = noLats;
// read in params
vector<double> params = ImportWarpFromNetCDF(warpName);
// rescale field to warp's dimensions, and read in
string tempName = "scaledField";
ReScale(fieldName, tempName, noLongs, noLats);
vector<vector<vector<double> > >inIntensities = ImportFieldFromNetCDF(tempName);
RemoveFile(tempName);
// just enter inIntensities for ref image, and 1 for lambda, to keep objective function happy
ObjectiveFunction objective(inIntensities, inIntensities, conserve, 1, false);
objective.setParameters(params);
// output files
ExportOutputToNetCDF(objective, outName);
cout << "BAH?!" << endl;
}
where the cout line at the end was just checking to see I'd got to the end of the function properly (which I have). Any thoughts on why this would be segfaulting here? I appreciate it might be hard to tell without seeing what the individual function calls do, and so I'll add those if necessary.
It doesn't actually matter too much, as this function is the last thing to be called (so the seg fault doesn't interrupt anything), but I still would rather get to the bottom of it!
The only thing that happens "after" the function are destructor calls. Check all your destructors of local variables. It looks like ObjectiveFunction is the only local variable that's not a primitive or standard library container, so check ObjectiveFunction::~ObjectiveFunction() for potential problems.