Use of global/static objects

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
hreintke
Posts: 6
Joined: Thu Oct 15, 2020 10:53 am

Use of global/static objects

Post 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.

User avatar
fpiSTM
Posts: 852
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 33
Location: Le Mans
Contact:

Re: Use of global/static objects

Post 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().

hreintke
Posts: 6
Joined: Thu Oct 15, 2020 10:53 am

Re: Use of global/static objects

Post 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.

User avatar
fpiSTM
Posts: 852
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 33
Location: Le Mans
Contact:

Re: Use of global/static objects

Post by fpiSTM »

Could you share your library ?

hreintke
Posts: 6
Joined: Thu Oct 15, 2020 10:53 am

Re: Use of global/static objects

Post 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.

hreintke
Posts: 6
Joined: Thu Oct 15, 2020 10:53 am

Re: Use of global/static objects

Post 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/

User avatar
fpiSTM
Posts: 852
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 33
Location: Le Mans
Contact:

Re: Use of global/static objects

Post 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()...

stevestrong
Posts: 390
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 7
Location: Munich, Germany
Contact:

Re: Use of global/static objects

Post by stevestrong »

This is why a good practice is to define an init() function for the class where you want to insert debug code.

Post Reply

Return to “General discussion”