Program failing when initializing a class no compile time errors

Status
Not open for further replies.

ValStorm

Member
Hello,

I'm working on a project on a Teensy 3.2 and Arduino IDE and everything was working great. All of the sudden when I initialize my class object and call the constructor it enters a dead zone and control never returns. So, I commented out all the implementation details and still nothing will execute beyond the line where I invoke the class.

So in my cpp file I have

Code:
Class::Class()
{
  //commented out code
}

and in my .ino

Code:
#include "Class.h"

Class thisClass = Class();

void setup()
{
  //stuff that wont happen
}

Everything compiles fine. Uploads fine. The serial monitor shows online. When I comment out the class invocation everything proceeds fine. I would think that an empty constructor would pass control right back to the program but this is not happening. I even tried throwing a "return;" in the empty constructor for kicks. Any ideas on what might be happening? What kind of things can get past compilers but cause such a problem? I'm really banging my head against the wall here.
 
After hours of debugging I tracked down the issue.

I had a member to store the last written element of a 3d array like this in my header file

Code:
 struct Location
    {
      uint8_t x;
      uint8_t y;
      uint8_t z;
    };

But where it got hung up was on
declaring an instance of that type as a member of my class.

Code:
Location last;

Is when everything gets weird. I tried changing the name of the type, names of the member variables and the name of the instance, i initialized the values and didn't initialize the values and while it would always let me define the type as soon as I invoked an instance of that in my class the whole program would break.

Why would this be?
 
It is known that there can be problems with constructors doing stuff too early - without a complete runnable sketch showing the issue and complete details the specifics beyond that will be too big a puzzle to be addressed
 
That might be a common issue which is called early initialization problem. That means that other objects, types, or variables called in the object constructor are not (yet) available when the object is declared and thus the constructor is called.

To prevent this, especially in the embedded world where often debug approches are limited, the paradigm, especially in the Arduino environment, is to leave the constructor as empty as possible and to add a begin() function which can be called later in the code when one has made sure before that all needed elements are already initialized.
 
Code:
Class thisClass = Class();

Might be wrong but as far as I understand, this will construct a dummy Class on the stack, then call the copy constructor to generate thisClass, and then call the destructor of the dummy class. I can think of no scenario where you would need this.
Did you mean

Code:
Class thisClass;

instead? Would be interesting to see a minimal example showing the behavior.
 
That might be a common issue which is called early initialization problem. That means that other objects, types, or variables called in the object constructor are not (yet) available when the object is declared and thus the constructor is called.

To prevent this, especially in the embedded world where often debug approches are limited, the paradigm, especially in the Arduino environment, is to leave the constructor as empty as possible and to add a begin() function which can be called later in the code when one has made sure before that all needed elements are already initialized.

I cleared out the constructor and added a begin() function or rather _begin() because begin came up as a reserved word and overloading stuff like that makes me nervous...

Code:
Class thisClass = Class();

Might be wrong but as far as I understand, this will construct a dummy Class on the stack, then call the copy constructor to generate thisClass, and then call the destructor of the dummy class. I can think of no scenario where you would need this.
Did you mean

Code:
Class thisClass;

instead? Would be interesting to see a minimal example showing the behavior.

Also went with:

Code:
Class thisClass;

void setup() {

  thisClass._begin();

}

...and everything is working just fine. Thank you all!

Is there any environment where you can see more detailed info about what's going on? Some kind of more verbose feedback at compile time?
 
Is there any environment where you can see more detailed info about what's going on? Some kind of more verbose feedback at compile time?

File > Preferences, look for "Show verbose output during" and click the "compilation" checkbox.

Arduino compiles your code in a temporary folder. Inside that folder, you can find a .lst and .sym file with detailed info about how the compiler generated the final code.
 
Status
Not open for further replies.
Back
Top