Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Program failing when initializing a class no compile time errors

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    10

    Program failing when initializing a class no compile time errors

    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.

  2. #2
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    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?

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    7,839
    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

  4. #4
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,299
    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.

  5. #5
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    378
    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.

  6. #6
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Quote Originally Posted by Theremingenieur View Post
    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...

    Quote Originally Posted by luni View Post
    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?

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,539
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •