Page 1 of 1

Use of global/static objects

Posted: Wed Nov 25, 2020 1:49 pm
by hreintke
LS,

Is there a limitation on the usage of static/global objects ?

When instantiating a global object from my local library the board freezes (both stm32f103 & stm32f411)'

Minimal sketch :

Code: Select all

#include "CommandProcessing.h"

CommandHandler* commandHandler = new CommandHandler; -> Fails
CommandHanlder commandHandler;                       -> Fails

CommandHandler* commandHandler; // + new in setup -> OK.
void setup() {
//  commandHandler = new CommandHandler;
  pinMode(PC13,OUTPUT);
  digitalWrite(PC13,HIGH);
}

void loop() {
  delay(500);
  digitalToggle(PC13);
}
The CommandHandler class has low memory usgae.
Did not get into the implementation details as I would like to know the eventual limitations first.

Re: Use of global/static objects

Posted: Wed Nov 25, 2020 2:15 pm
by fpiSTM
I don't know which library you used any you cannot use new at global.
For the second, I gues the constructor execute some code that's probably why it's failed.

Try to define global the CommandHandler* commandHandler;
then do the new in the setup().

Re: Use of global/static objects

Posted: Wed Nov 25, 2020 2:38 pm
by hreintke
Try to define global the CommandHandler* commandHandler;
then do the new in the setup().
Initializing it that way is working OK.

The library is my own, I am using it extensive in my esp8266 & esp32 projects without the limitation of only "new in setup".
No big constructor either.

Trying to find the root cause, so I can prevent future identical issues.

If there are no known limitations I will get into the details and hopefully get to a small sketch.

Re: Use of global/static objects

Posted: Wed Nov 25, 2020 2:45 pm
by fpiSTM
Could you share your library ?

Re: Use of global/static objects

Posted: Wed Nov 25, 2020 3:04 pm
by hreintke
Yes, I can share but overall it is a complex one with combined esp/stm support.

Let me first try to make a minimal version of it by stripping and shortcutting.

Re: Use of global/static objects

Posted: Fri Nov 27, 2020 1:36 pm
by hreintke
Hi,

I found the root cause.
In the constructor I had and old debug print statement to Serial1.

For completeness, I have options
- USB support : None
- U(S)ART : Enabled (generic Serial)

Small sketch to reproduce the issue

Code: Select all

#include "Arduino.h"
#include "srcWrapper.h"

class CommandHandler
{
public:

	CommandHandler()
	{
//		Serial1.printf("test");  //--> Instantiation fails when uncommented
	}
};

CommandHandler commandHandler;

void setup()
{
	pinMode(LED_BUILTIN,OUTPUT);
	digitalWrite(LED_BUILTIN,LOW);
}

void loop()
{
	delay(500);
	digitalToggle(LED_BUILTIN);
}
I do not (yet) have the detailed knowledge of the stm32duino core to investigate further.
But if you can give some hints on where to look, maybe I can help/

Re: Use of global/static objects

Posted: Fri Nov 27, 2020 4:13 pm
by fpiSTM
Thanks for feedback.

You canno't use Serial at this time as instantiation is done before the sytem init (hal init, clock config) and also Serial1 can simply not be instantiate at this time and also not configureed by the Serial.begin()...

Re: Use of global/static objects

Posted: Fri Nov 27, 2020 4:42 pm
by stevestrong
This is why a good practice is to define an init() function for the class where you want to insert debug code.