This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C++ Object Lifetime in main()

Other Parts Discussed in Thread: SYSBIOS

Hi

I'm working with SYS/BIOS 6.32 on a F2808. I'm puzzled by the lifetime of C++ objects instantiated in main().

What happens to main() after BIOS_start is called? The documentation seems a little vague - I've seen comments in code samples that suggest main never exits, which is what I would expect. Certainly if I put a breakpoint at the end of main, it never gets hit.

But what is puzzling me is that objects I instantiate in main, before calling BIOS_start, seem to get destroyed somewhere - if I set a breakpoint in their destructors, it gets hit, as if they were going out of scope.

A pointer to any documentation would be hugely helpful - I have looked, but II can't find anything.

Many Thanks

Tony

  • Hi Tony,

    I just tried to do the same with the CCS C++ (bigtime) example, but I didn't see the destructor being called before BIOS_start().

    Can you provide some snippet or .c file where this happens?

    I just took the SYS/BIOS bigtime example and modified the main() function as follows:

    /*
     *  ======== main ========
     */
    int main()
    {
        Clock test(5);
        Clock *ptrtest;

        ptrtest = new Clock(1);

        ptrtest->tick();

        Log_info0("bigTime started.");
        
        BIOS_start();                                     
    }

    In either instance I didn't see Clock::~Clock() being called (using a breakpoint and a while(1) loop). I ran this example on a C28 core.

    Which version of SYS/BIOS 6.32 are you using?

  • Tom

    Thanks for your help. I'm sorry - I can't post any examples because something I have done in the meantime (without changing my source) has fixed it. Possibly something in the .cfg - that is all I changed!

    Story to date:

    I've started from the "SYSBIOS HWI Example for C2000" from the Wiki (which is based on the idle sample project. All OK.

    Written a Hwi "despatch table" class and a "PWM device handler" class to test Hwi processing in C++. Put the despatch table in static memory outside main, instantiated the "device handler" as an auto object in main. Experienced the problem I described to you above. First Hwi received, but never gets handled by the device handler. Device handler member pointers all NULL, tracked down that the destructor was getting invoked from somewhere. Not sure I can believe what I'm seeing - debugger is happilly letting me set breakpoints in a class that has been destroyed!

    Changed code to instantiate device handler class dynamically via new from within main. Didn't work at first - realised I still had default heap set to zero size in the cfg. Changed that to 4096. Everything works fine, device handler processing PWM hwi's  faultlessly.

    Go back to instantiating as an auto in main so that I could send you some sample code. Works perfectly!

    I can't see that anything has changed except the .cfg, and even if I put that back the way it was, everything works still.

    Sorry if I've sent you chasing wild geese, but I was reliably seeing the destructor called, hence my query about lifetimes. Now I can't get back to the same condition.

    The only explanation I can come up with is a bug somewhere in my code (not found one) that despatched the hwi somewhere disastrous and corrupted a stack or something.

    If I ever get to the bottom of it and find something worth sharing, I'll report back here. In the meantime, thanks for your help, I hope I haven't wasted your time, and I'll flag it as closed.

    Regards

    Tony