Used variables are optimized out

Hi,
I have been happily debugging my ESP32 project but have started getting optimized out variables in places where they are needed for the code.

The commented out “debug build flags” is what has been working. The line below it is my latest guess. I have read the compiler doco and tried numerous variations to this line but am not making progress. I either get build errors, the “dangerous relocation and link” error, or debugging with missing variables.

Any insight as to what might be going on would be appreciated.

Thanks,
Tony

This message can misleading. It can also mean that variable does not exist yet or not exist anymore.

As an example: when stepping through the below code, the variable a probably only exists before line 5 is executed. Before that, it does not exist because it has never been assigned. After that, after line 5 it no longer exists as it is not needed anymore.

 1: void someFunction(int p, int q) {
 2:     int a;
 3:     ff3(q, 20);
 4:     a = ff4(p, 30);
 5:     ff5(a, 40);
 6: }

There shouldn’t be any need to change the build flags for debugging. For debugging, the project is automatically recompiled with debugging information.

You have both -Og and -Os in there… Remove at least -Os but keep -Og. If it still not working, replace it with -O0.

Hi manuelbl,

It’s not that, exactly. I was wondering if the logic of the code made the variable unnecessary. So stepping through the procedure with the variable in the watch window it suddenly went “unavailable” even though it was still used later in the procedure. Then after a few more steps the code went back to near the start of the procedure.

My procedure is queue processing for received serial packets. The que is filled in an interrupt process and dispatched in the main loop. Even though I am stopped in the debugger can some of this be still going on?

Tony

Thanks both of you!

I think I am getting some insight on what is going on… Let me know if it seems feasible.

  1. The unexpected jump back to near the start of the procedure… the debugger went back to where two variables were initialized. It took the proper values and changed them from “optimized out” to the correct value in the watch. I guess this is the debuggers way of getting debug data just in time. (but disturbing)

  2. My problem variable that stays optimized out. It is an index to an array. The code seems to run as though the variable were valid so maybe it is a bug that the debugger shows it as optimized out.

  3. On maxgerhardt’s comment… my original setting was with -Og and no -Os. I think I tried -o0, but I am going to run to my setup and try again, just in case.

Thanks,
Tony