Eclipse debug with MinGW: Failed to execute MI command - c++

I did a new installation of Eclipse Juno 32 bits, and a new installation of MinGW 32 bits, my platform is Windows 7 64 bits. When trying to debug a simple program I can watch very simple expressions, but everything more complicated gets me an error. For an example in the following program:
int main()
{
vector<int> vRings;
for(int i=0;i<50;i++) {
vRings.push_back(i%5);
}
//std::cout << "result:" << getRingNumber(vRings,vDiscs);
return 0;
}
In the watch window, watching vRings work normally, but trying to watch the content of the vector doesn't work:
Trying to watch vRings[0]:
Error: Multiple errors reported.\ Failed to execute MI command: -var-create - *
vRings[0] Error message from debugger back end: Could not find
operator[].\ Unable to create variable object
Trying to watch vRings.at(0):
vRings.at(0) Error: Multiple errors reported.\ Failed to execute MI
command: -var-create - * vRings.at(0) Error message from debugger back
end: Cannot evaluate function -- may be inlined\ Unable to create
variable object
What can I do to solve the problem?

You are trying to watch the result of a function call.
Both vRings.at() and vRings[] are functions (the latter an overloaded function) that return a reference to a value. In your case - an integer.
If you want to watch the value of vRings at index 0, you can try to assign it to a variable at some point, for example:
int main()
{
vector<int> vRings;
for(int i=0;i<50;i++) {
vRings.push_back(i%5);
}
int watchme = vRings.at(0);
//std::cout << "result:" << getRingNumber(vRings,vDiscs);
return 0;
}
Then you can watch the variable watchme.

Related

Access violation error in debug mode but application runs in release mode for a dll compiled in visual studio2015

I am using visual studio 2015 in 32bit mode to compile a dll(VSDll) which calls the functions written in another dll (MATLAB function exported as dll by MATLAB C++ compiler). This VSDLL is then called by an .exe file. Now, I can successflly compile the code into debug and release DLLs. I can also successfully run the release dll from the .exe file. It runs without any problem. But when I try to run the code in debug mode in visual studio I get the following error Error message.
This figure has the register information and the stack frame where the error occurs.Stack Frame and registers. Also attached the stack trace information here. Stack trace.
This might be a silly error but I am new to C++ and I don't have a very deep understanding of memory heaps and stacks. I tried enabling/disabling different settings in the debug mode as suggested in other answers which have worked for others but nothing works in my case. I was using the Visual studio professional version before and I could debug without this error. Now recently I had to change to visual studio community edition and since then I have this problem only when I try to set breakpoints in my code and debug it. Could this be the problem? Another thing I have noticed is, I am using Visual studio to compile various MATLAB functions as dll and use them to build customized dlls to run in TRNExe. Each of it works fine in the release mode, but in debug mode, everytime the same boost_log-vc110-mt-1_49.dll that breaks down and at the same memory register 0x7e37a348.
Can anyone please help me solving this error? I appreciate any ideas or suggestions regarding what the problem could be.
Please see the warning messages here. Could this be the source of the problem?Warning message
I have produced a minimal reproducible example below. It's still free lines of code. Another bottleneck for this is, I am trying to co-simulate between Trnsys and MATLAB. Trnsys has it's components written as Type DLLs (Fortran or C++) and it has a structure for this dll. I am trying to export the MATLAB function as dll, call it in the Trnsys type at required parts of the code and compile it as the DLL to run in Trnsys simulation studio. So, C++ links the MATLAB dll to the TRNDll which is the TRNSYS kernel library. I don't know how to do that as a minimal reproducible example. Anyways, I have compiled the MATLAB code to add two numbers and call it in the Type201.cpp(Respecting the TRNSYS structure) and compiled it into the DLL to run in TRNSYS studio. It works there. But when I try to run the Type201.cpp in debug mode, I still get the exact same error as before at the exact stack frame and the register with boost_log-vc110-mt-1_49.dll.
Type 201.cpp code is below
extern "C" __declspec(dllexport) void TYPE201(void)
{
double a;
double b;
double Timestep, Time, StartTime, StopTime;
int index, CurrentUnit, CurrentType;
mxArray* x_ptr ;
mxArray* y_ptr ;
mxArray* z_ptr = NULL;
double* output = NULL;
//Get the Global Trnsys Simulation Variables
Time = getSimulationTime();
Timestep = getSimulationTimeStep();
CurrentUnit = getCurrentUnit();
CurrentType = getCurrentType();
StartTime = getSimulationStartTime();
StopTime = getSimulationStopTime();
//Set the Version Number for This Type
if (getIsVersionSigningTime())
{
int v = 17;
setTypeVersion(&v);
return;
}
//Do All of the Last Call Manipulations Here
if (getIsLastCallofSimulation())
{
addlibTerminate();
mclTerminateApplication();
return;
}
//Perform Any "End of Timestep" Manipulations That May Be Required
//Do All of the "Very First Call of the Simulation Manipulations" Here
if (getIsFirstCallofSimulation())
{
//Tell the TRNSYS Engine How This Type Works
int npar = 2;
int nin = 2;
int nder = 0;
int nout = 1;
int mode = 1;
int staticStore = 0;
int dynamicStore =1;
setNumberofParameters(&npar);
setNumberofInputs(&nin);
setNumberofDerivatives(&nder);
setNumberofOutputs(&nout);
setIterationMode(&mode);
setNumberStoredVariables(&staticStore, &dynamicStore);
return;
}
//Do All of the "Start Time" Manipulations Here - There Are No Iterations at the Intial Time
if (getIsStartTime())
{
index = 1; a = getInputValue(&index);
index = 2; b = getInputValue(&index);
if (!mclInitializeApplication(NULL, 0))
{
//fprintf(stderr, "Could not initialize the application.\n");
exit(1);
}
if (!addlibInitialize())
{
//fprintf(stderr, "Could not initialize the library.\n");
exit(1);
}
//Read in the Values of the Inputs from the Input File
return;
}
if (getIsEndOfTimestep())
{
return;
}
//---------------------------------------------------------------------------------------------------------------------- -
//ReRead the Parameters if Another Unit of This Type Has Been Called Last
//Get the Current Inputs to the Model
index = 1;
a = getInputValue(&index);
index = 2;
b = getInputValue(&index);
int noutput = getNumberOfOutputs();
//Create an mxArray to input into mlfAdd
x_ptr = mxCreateDoubleMatrix(1, 1, mxREAL);
y_ptr = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy(mxGetPr(x_ptr), &a, 1 * sizeof(double));
memcpy(mxGetPr(y_ptr), &b, 1 * sizeof(double));
mlfAdd(1, &z_ptr, y_ptr, x_ptr);
output = mxGetPr(z_ptr);
index = 1;
setOutputValue(&index, output);
return;
}
Then this is the MATLAB function to add two numbers.
function [s] = add(a,b)
s = a+b;
end
I complied this into dll via command line using
mcc -B csharedlib:addlib add.m
This is likely because some std classes and boost classes have different layout for release and debug builds. In debug builds additional members are inserted via macros to enable better debugging experience like iterator debugging and such. So as documented by Microsoft it is undefined behavior to mix code linked to different c++ runtime. The matlab dll is build in release mode and linked to c++ release runtime so it shouldn't be used by code linked to debug libraries!

LuaBridge getGlobal is always returning nil

I did a first small test with LuaBridge 1 week ago and it worked to get an int from the script.
Now I deleted this code and tried to include Lua script in my game engine but it is no longer working.
I tried to go back to a basic code with this :
#include <iostream>
#include "lua5.2/lua.hpp"
#include "LuaBridge/LuaBridge.h"
using namespace luabridge;
int main()
{
lua_State* L;
L = luaL_newstate();
if(!luaL_loadfile(L, "../../script.lua"))
std::cout << "failed loading" << std::endl;
LuaRef s = getGlobal(L, "nmbr");
int luaInt = s.cast<int>();
std::cout << luaInt << std::endl;
return 0;
}
with this script
nmbr = 30
And it gives me :
PANIC: unprotected error in cell to Lua API (bad argument #2 (number expected, got nil))
Aborted (core dumped)
It the same when I tried to get a string or a function from the script and I have no idea what I'm doing wrong in this.
Thanks for your answers :)
From the documentation of luaL_loadfileex:
As lua_load, this function only loads the chunk; it does not run it.
What that means is that the script is loaded, but it haven't been executed so there is really no variable nmbr to get. You need to run the script first for the code to work (for eample by calling lua_call).
This is shown very well in the first simple example in this LuaBridge tutorial.
luaL_loadfile ~= luaL_dofile. You load the script and get it as function on the stack but don't execute it, so the global assignment doesn't happen.

Tracing variables in Lua for read access to start user defined C++ method/function

I'm in the process of evaluating script language interpreters to be embedded in C++ applications. TCL/cpptcl and Lua are in my focus right now. TCL has a nice feature that enables me to "trace variable accesses". So whenever I read a defined variable, my read-callback function is triggered:
int read_trace(const int& v, void *) {
cout << "read trace triggered" << endl;
return v;
}
...
void tclInterpreter() {
std::cout << ": Starting TCL interpreter!" << std::endl;
// new TCL
Tcl::interpreter i;
i.def_read_trace("tracedVar", "read", read_trace);
// load the script
ifstream script("helloworld.tcl");
// run the script with the given arguments
i.eval(script);
}
So if I now execute the following TCL from my C++ application:
set tracedVar 10
for { set i 0 } { $i <= 5 } { incr i } {
puts $tracedVar
}
I receive the output:
read trace triggered
10
read trace triggered
10
read trace triggered
10
read trace triggered
10
read trace triggered
10
read trace triggered
10
So I get execution of my variable read callback and then the puts-value of the var.
QUESTION: Can I do this with Lua and if yes how? I did not find any direct topic on this. Only thing that caught my eye was about debugging Lua, where a debugger (debugging API) would watch the value of a variable. But I do not want to watch for value changes, but for value access.
I think the closest thing to this in lua is an __index metamethod on a table (with a check for the specific key if you only want to trigger on one).
This could even be done on the default global table (_G) if desired.

Applications keeps closing vs 2012

My application keeps closing when debugging. I'm not able to view what the "results" are since it goes too fast.
I've been looking at many different forums and topics and all the solutions given just wont apply. I've tried different commands before returns 0; etc and also changing an option in the project.
I'm just starting and trying to learn from the c++ primer but this is frustrating me already :).
Following is my code, please help!
#include <iostream>
int main ()
{
int sum = 0, val = 1;
while (val <= 10) {
sum +=val;
++ val;
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
Console.Read();
return 0;
}
Don't do Console.Read();, do std::cin.get();.
Try this:
#include <iostream>
int main ()
{
int sum = 0, val = 1;
while (val <= 10) {
sum +=val;
++ val;
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
std::cin.get(); // hackish but better than system("PAUSE");
return 0;
}
Assuming that you are using Visual Studio:
Debug builds will run until they hit a breakpoint or until the program finishes (which ever comes first). If the program finishes, the console closes. Place a break point on the line containing return 0; and your console will stay open until you click Continue.
Release builds will run until the program finishes. If the program finishes, you will be prompted to Press any key to continue. . . and the console will stay open.
If you are not setting breakpoints in such a small program, you are wasting your resources -- debug mode will impact the performance of the program.
Thus, you should build in Release mode and forget about using std::cin.get().

conditional display in gdb

I'm using gdb to debug some c++ code. At the moment the code I'm looking at iterates through an array of pointers, which are either a pointer to some object or a NULL pointer.
If I just display list[index]->member it'll complain when list[index] is null. Is there anyway to display the member only if list[index] is not null? I know you can set conditional breakpoints (condition <bp-num> <exp>) but I'm not sure how that'd help.
The code in question is:
for (int i=0;i<BSIZE*BSIZE;i++){
if (vms[i]==target) {valid=true; break;}
}
where vms is the array of pointers.
Since display accepts arbitrary expressions, you can try something like the following display command:
display (list[index]) ? list[index]->member : "null"
I'm not sure if that cleans things up well enough for what you want - you'll still get a display, but it won't be a complaint.
Basically the condition works like this:
#include <iostream>
int main() {
for (int i=0; i<10; ++i) {
std::cerr << i << std::endl;
}
}
You can debug it like this:
(gdb) break 5
Breakpoint 1 at 0x100000d0e: file foobar.cpp, line 5.
(gdb) condition 1 i==3
(gdb) r
Starting program: /private/tmp/foobar
Reading symbols for shared libraries ++. done
0
1
2
Breakpoint 1, main () at foobar.cpp:5
5 std::cerr << i << std::endl;