Skip to content

Why would calling attachInterrupt in an constructor fail to work for some builds #2569

Closed Answered by Alan01252
Alan01252 asked this question in Q&A
Discussion options

You must be logged in to vote

Understood @fpistm , googling around etc I think you're right.

Reading main.cpp of stmduino

// Force init to be called first, i.e. before static object allocation.
// Otherwise, statically allocated objects that need HAL may fail.
attribute((constructor(101))) void premain()
{
// trim
init();
}

It sounds like for most scenarios this would work, but I can see how interrupts might act differently.

I hadn't for example considered the case of the interrupt firing before the constructor had completed and I can see how that might cause various problems too.

But in general I'm now leaning towards the fact that almost everything related to GPIO pins etc should be done in a begin function.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Alan01252
Comment options

@fpistm
Comment options

@Alan01252
Comment options

Answer selected by fpistm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants