Uno- Debug. Code is being ignored in debug mode

When in debug mode, all if () statements, and int declarations are ignored. String declarations are executed.

If I use Serial.println() I can see these statements are executed.

I have created a demo program that uses conditional compilation to demonstrate the problem. Uncomment or comment out the line #define DEBUG 1 to see the problem.

Here is the code.

#include <Arduino.h>
#include <avr8-stub.h>

//*************************************
//  uncomment this line to run in debug mode.
//#define DEBUG 1
//************************************
void setup()
{
 #ifdef DEBUG
  debug_init();
#else
  Serial.begin(9600);
#endif
}

void loop()
{
// put your main code here, to run repeatedly:
#ifdef DEBUG
  breakpoint();
#endif
  String str = "This is a test";
  int x;
  x = 1;
  if (str != "")
  {
     x = 1;
#ifndef DEBUG
    Serial.println("Checked string if");
#endif
  }

  if (1 == 1 )
  {
    int x = 1;
#ifndef DEBUG
    Serial.println("Checked X IF");
#endif
  }
#ifndef DEBUG
  delay(3000);
#endif
}

?

What does it show for you.

avrdude: verifying …
avrdude: 6766 bytes of flash verified

avrdude done. Thank you.

========================= [SUCCESS] Took 6.14 seconds =========================
Reading symbols from c:\Users\Colin\Documents\PlatformIO\Projects\Mill Controls V1.pio\build\uno\firmware.elf…
done.
PlatformIO Unified Debugger → Redirecting...
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target…
loop () at src/main.cpp:22
22 String str = “This is a test”;
PlatformIO: Initialization completed
PlatformIO: Resume the execution to debug_init_break = tbreak main
PlatformIO: More configuration options → Redirecting...

Program received signal
SIGTRAP, Trace/breakpoint trap.
loop () at src/main.cpp:22
22 String str = “This is a test”;

Program received signal
SIGTRAP, Trace/breakpoint trap.
loop () at src/main.cpp:22
22 String str = “This is a test”;

Program received signal
SIGTRAP, Trace/breakpoint trap.
loop () at src/main.cpp:22
22 String str = “This is a test”;

This is in debug mode. In non-debug it runs as expected

The code may be so simple that is optimizes these simple assignments and 1 == 1 away with no further thought, especially since without DEBUG the calls to actual work-doing functions like Serial.println() is gone. Add

debug_build_flags = -O0 -g -ggdb3

to the platformio.ini and retry. Does it behave differently?

Yes that fixed it.

The original program it came out of was much more complex but did the same thing. What I was trying to do here was simply demonstrate the issue. I guess I was successful.

Thank you for the help.