-
-
Notifications
You must be signed in to change notification settings - Fork 7k
atexit() called after setup() #1919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm not sure what you mean here. I don't see any You also mention undefined references while linking and invalid memory, which are two completely separate issues. I suspect you just have a problem in your code that you're misinterpreting as an Arduino bug. Could you paste a complete (but as small as possible) sketch that displays the problem and describe what happens and what you had expected to happen? |
ok...as i promised a more verbose explanation ;) if a singleton is definded in the follwoing way:
the linker will complain about an undefined/unresolved symbol for atexit(). If a correct implementation of atexit is provided in the user code (to fix the unresovled symbol)
the code will link, but at atexit is beeing called after the completion of the setup method. If func() is executed, it will invoke the destructor of the singleton-class and leaving you behind with an invalid reference in the loop() or on the next invovaction of instance(). |
Right. Your implementation of I'm surpised that the compiler actually inserts calls to atexit - you'd expect that it can more efficiently call the destructors without using a function pointer and (thus) an indirect function call. However, thinking about this more, I think that the static variable you declared will not be initialized at startup, but only when the In any case, if you supply an empty |
Oh, one more thing: If you paste code, make sure to indent it at least 4 spaces or a tab, or put triple backticks (```) before and after the block of code, to get a fixed-width font. |
i just checked the c/c++ reference again... oops...you are right about the wrong implementation - sorry, my bad - seems i forgot to many thing within the last 5years i didn't use c++ ;) yes, the static variable won't be initialized until the first call of instance(). One could provide an different, pointer based implementation - but this would produce more overhead
I "solved" the problem as you said with an empty implementation of atexit since atexit() doesn't make sense in the Arduino-Environment anyway. But as you suggested, an empty default implementation would be nice - maybe with an attribute weak, so in the unlikely case it's needed one can provide a different implementation |
thanks for the hint with the 4 spaces ! |
Perhaps you could supply a pullrequest that adds a weak and empty |
Fixed with 1bbcb2f |
This is an empty stub to simply allow use of complex types with a non global static lifetime. For more complex handling the function 'atexit' can be redefined in user code. For more information see: arduino/Arduino#2229 arduino/Arduino#1919
This is an empty stub to simply allow use of complex types with a non global static lifetime. For more complex handling the function 'atexit' can be redefined in user code. For more information see: arduino/Arduino#2229 arduino/Arduino#1919 (Filtered from arduino/Arduino@1bbcb2f)
This is an empty stub to simply allow use of complex types with a non global static lifetime. For more complex handling the function 'atexit' can be redefined in user code. For more information see: arduino/Arduino#2229 arduino/Arduino#1919
This is an empty stub to simply allow use of complex types with a non global static lifetime. For more complex handling the function 'atexit' can be redefined in user code. For more information see: arduino/Arduino#2229 arduino/Arduino#1919
This is an empty stub to simply allow use of complex types with a non global static lifetime. For more complex handling the function 'atexit' can be redefined in user code. For more information see: arduino/Arduino#2229 arduino/Arduino#1919
This is an empty stub to simply allow use of complex types with a non global static lifetime. For more complex handling the function 'atexit' can be redefined in user code. For more information see: arduino/Arduino#2229 arduino/Arduino#1919
This is an empty stub to simply allow use of complex types with a non global static lifetime. For more complex handling the function 'atexit' can be redefined in user code. For more information see: arduino/Arduino#2229 arduino/Arduino#1919
First of all, there is no atexit() definition provieded by the runtime-environment which gives you, in case of "stack-based" singleton implementation - undefined references while linking.
class Singleton {
static Signleton& instance() { static Signleton theInstance; return theInstance; }
};
Secondly, the atexit is called after leaving setup() - which if implemented correctly - will just delete the instance and leave you with invalid memory!
The text was updated successfully, but these errors were encountered: