Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 8 Replies) Exceptions for new if no allocation takes place (Read 597 times)
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Exceptions for new if no allocation takes place
Jan 4th, 2025 at 3:19pm
Print Post  
According to the C++ standard, an exception has to be thrown
if new cannot allocate memory. Therefore
Code (C++)
Select All
      int* p = new int;
      delete p;
      int n = 1;
      int rep = 1;
      Serial.println("\nStart N_Exception_Basics::test_too_much_news(mit_Exceptions) ");
#ifdef __cpp_exceptions
      try
      {
        while (p!=nullptr)
        {
          p = new int[n];
          delete[] p;
          n = n * 10;
          rep++;
          Serial.printf("n=%d p=%d \n", n,(int)p);
        }
      }
      catch (std::exception& e)
      {
        Serial.printf("Exception bei test_too_much_news n=%d what()=%s\n", n, e.what());
      }
      catch (...)
      {
        Serial.printf("... Exception bei test_too_much_news \n");
      }
      Serial.printf("nach catch ... Exception bei test_too_much_news \n");
 



should never return p==0, but throw a badalloc exception if memory could not be allocated.

However, for an Raspberry Pico 2 and an STM32 I get results like
Code (C++)
Select All
Start N_Exception_Basics::test_too_much_news(mit_Exceptions)
n=10 p=536892088
n=100 p=536894464
n=1000 p=536894464
n=10000 p=536894464
n=100000 p=536894464
n=1000000 p=536894464
n=10000000 p=0
n=100000000 p=0
n=1000000000 p=0
Exception bei test_too_much_news n=1000000000 what()=std::bad_array_new_length
nach catch ... Exception bei test_too_much_news  



where p=0 results should not be.

For an ESP32 board I get no such p=0 results.

Do I need special compiler flags so that a I get a badalloc exception? Or is there something wrong with the compiler?

Thanks
Richard


  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12167
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Exceptions for new if no allocation takes place
Reply #1 - Jan 4th, 2025 at 5:18pm
Print Post  
I suggest raising the question on the forum for the board package you have used. Visual Micro and the Arduino IDE read the compiler instructions from the package then run the commands specified by those instructions. That means that the board package authors decide how things work.

If the authors provide switches then we can tell you how to make use of them.
  
Back to top
IP Logged
 
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Re: Exceptions for new if no allocation takes place
Reply #2 - Jan 10th, 2025 at 6:48pm
Print Post  
Hi Tim,

I raised this question to the STM32duino.com forum
https://www.stm32duino.com/viewtopic.php?p=15167#p15167

They answered me, 
Quote:
Well, no compiled version is provided, only source. You build on your side and you can override yourself the compilation options thanks *.local files options from Arduino, see the Arduino platform specification for this.


Can you please tell me where to set these flags in the Visual Micro Extension?

Thanks
Richard
  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2564
Joined: Feb 13th, 2019
Re: Exceptions for new if no allocation takes place
Reply #3 - Jan 10th, 2025 at 6:55pm
Print Post  
Thanks for the update and link to the ticket.

The options can be set in the Project Properties (vMicro > Project Properties) under the Configuration Extra Flags.
  

Please Register or Login to the Forum to see File Attachments
Back to top
IP Logged
 
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Re: Exceptions for new if no allocation takes place
Reply #4 - Jan 11th, 2025 at 2:03pm
Print Post  
Hi Simon,

your answer sounds absolutely reasonable.

However, it does not work for me for a Nucleo64 board with an STM32 F303RE. No matter, if I set only the "Configuration Extra Flags" to -fexceptions, or all of "Configuration Extra Flags /Core/Libraries/Project" to -fexceptions, I always get no exception when I want to allocate too much memory with new.

Thanks
Richard
  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2564
Joined: Feb 13th, 2019
Re: Exceptions for new if no allocation takes place
Reply #5 - Jan 11th, 2025 at 4:54pm
Print Post  
Thanks for the update.

The patterns in the STM32 board package already include the "-fno-exceptions" which will be overriding your entries in the project properties.

Assuming you are using the v2.9.0 version of the STMicroelectronics board package:
1) Remove the entries in the Project Properties box (to avoid confusion as they are obsolete in this scenario)
2) Add a Local Board.txt to the project (vMicro > Add Code > Add Local Board.txt)
3) Paste in the contents of the attached file
4) Restart Visual Studio and build and upload again

This is just overriding the "compiler.cpp.flags" pattern which can be seen in the output when vMicro > Compiler > Show Build Properties is enabled.

If you are using a different board package version, or if you need this to be a system wide change let us know (there is an alternative way to override this using the platform.local.txt file which is injected into the board package folder).
  

Please Register or Login to the Forum to see File Attachments
Back to top
IP Logged
 
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Re: Exceptions for new if no allocation takes place
Reply #6 - Jan 11th, 2025 at 5:24pm
Print Post  
Hi Simon,

yes, I am using the v2.9.0 version of the STMicroelectronics board package. 

I did all your steps from 1) to 4).

However, it does not work for me for a Nucleo64 board with an STM32 F303RE. I still always get no exception when I want to allocate too much memory with new.

Thanks
Richard
  
Back to top
 
IP Logged
 
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Re: Exceptions for new if no allocation takes place
Reply #7 - Jan 12th, 2025 at 1:44pm
Print Post  
Hi Simon,

to avoid the quite complicated steps from your 

Reply #5 - Today at 4:54pm 

I suggest that you pass the '-fexceptions' flag to the compilation of the libraries for new etc. 

ESP32 projects already work this way. They throw an exception for 

Code (C++)
Select All
std::vector<int> v;
int too_much_push_backs()
{
  Serial.printf("Start too_much_push_backs() \n" );
  while (true) // leave at an exception
  {            //        from push_back
    v.push_back(1);
    if (v.size() % 1000 == 0)
      Serial.printf("  v.size()=%d \n", v.size());
  }
} 



I consider it highly counterintuitive to turn exception handling on, but to get it only for a part of the code, with the final effect, that I don't get it at all for e.g. push_back.

Thanks
Richard


  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2564
Joined: Feb 13th, 2019
Re: Exceptions for new if no allocation takes place
Reply #8 - Jan 12th, 2025 at 2:34pm
Print Post  
Thanks for the update.

Quote:
I suggest that you pass the '-fexceptions' flag to the compilation of the libraries for new etc.

This is up to the board package authors to implement as a default behaviour.  The local board.txt override is as simple as it can be made (or platform.local.txt in ArduinoIDE) as the board package authors have already hardcoded the -fno-exeptions into the build patterns in the board package.

Please attach the complete build log with the boards.txt override for the STM32 board so we can check it has applied to the project/library/core code correctly.

If you can attach the log for the ESP32 where it works as expected it will help us understand the packages and boards in use fully.

Just to note that the compiler used in ESP32 and STM32 are separate and implemented by the board package authors for their respective architectures.  As they are running on microprocessors not all of the standard C/C++ functionality is always included, and it can differ from package to package.  This is not to say it cannot work, but that the default behaviour in Arduino can differ between the two, as well as between standard C/C++ as on a full PC.
  
Back to top
IP Logged
 
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Re: Exceptions for new if no allocation takes place
Reply #9 - Jan 12th, 2025 at 2:56pm
Print Post  
Hi Simon,

attached are the build properties for an
STM32-F303RE
Raspberry RP-2350
ESP32

Best
Richard
  
Back to top
 
IP Logged
 
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Re: Exceptions for new if no allocation takes place
Reply #10 - Jan 12th, 2025 at 3:09pm
Print Post  
Hi Simon,

I just can't find the button to attach the build properties.

I will send them per mail to sales@visualmicro.com

Richard
  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2564
Joined: Feb 13th, 2019
Re: Exceptions for new if no allocation takes place
Reply #11 - Jan 14th, 2025 at 2:47pm
Print Post  
Thanks for the logs and the updates.

The behaviour differs between these boards due to the very different architectures and SDKs which are used in the toolchains production, and the flags enabled for their respective compilers, as well as it being an embedded environment.

I believe exceptions are not always thrown or are disabled at compile time (in the case of STM32) due to limited resources and increased latency when handling exceptions.  It is also important to note the "Standard C/C++" libraries are not complete on microcontrollers due to the code space and resources availablle.

RP2350
On the RP2350, it never throws the bad_alloc() exception, which digging into the "new" function using a Hardware Debugger seems to be due to the PicoSDK Build (used within the core) having PICO_CXX_ENABLE_EXCEPTIONS set to 0.  There also appears to be some compiler optimization interfering with the null pointer, which is improved with the -f-no-delete-null-pointer-checks compiler flag (which can be specified in the Project Properties in vMicro).

With the -f-no-delete-null-pointer-checks flag added only one instance of p=0 is shown (so exits at the right time), but no bad_alloc exception is thrown, however the while() loop in your code will exit as p == nullptr.

This can then be caught in an if statement, and manually thrown as bad_alloc if desired, as p == 0 || p == nullptr do mean the allocation failed.  You could implement this approach as an overridden new handler if desired.

STM32
Using an STM32 Nucleo F103RB (I dont have your exact model), and using the -fexceptions flag at compile time the too_much_push_backs() example given, does generate an exception, however this then jumps to the Default_Handler, which is defined as an infinite loop.  The only way to see futher information about an uncaught exception is using a debug probe, which some boards have builtin and can be used within Visual Micro.

Current Conclusion
The board package authors are the best place to get detail about this depth of the implementation of the GNU C/C++ specification embedded in their Arduino board packages for each architecture, and may be different to the standard C/C++ implementations seen on desktops etc... where the full standard can be adhered to.
  
Back to top
IP Logged
 
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Re: Exceptions for new if no allocation takes place
Reply #12 - Jan 15th, 2025 at 10:19pm
Print Post  
Hi Simon,

thanks for your answer. 

In the meantime I got some helpful hints
Quote:
https://www.stm32duino.com/viewtopic.php?p=15201#p15201
  by fpiSTM » Wed Jan 15, 2025 10:30 am. 

with the conclusion
Quote:
To sum up if you want exception support using Arduino IDE or cli (other ide, tools,... are not supported here):
- override compiler.cpp.flags in a platform.local.txt to replace -fno-exceptions by -fexceptions
- use the Newlib standard.


Please tell me where I can set the compiler flags. In VisualMicro, I only found the possibility to add boards.txt using vMicro|Add Code|Add Local board.txt. I could not find a platform.txt.

Thanks 
Richard
  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2564
Joined: Feb 13th, 2019
Re: Exceptions for new if no allocation takes place
Reply #13 - Jan 15th, 2025 at 10:59pm
Print Post  
The override can be applied in the Local Board.txt for each project (vMicro only).

To override the entire board package (for all projects using it in vMicro + ArduinoIDE), you would need to go to the folder where the current package is installed and create a platform.local.txt file, with the overridden pattern in.

Normally this would be under "%LOCALAPPDATA%\Arduino15\packages\rp2040\hardware\rp2040\4.0.2" for v4.0.2 of the RP2040/2350 Board Package (but there are various ways to reconfigure the root Arduino15 folder so its not a gaurentee).

If unsure you can enable "vMicro" > "Compiler" > "Show Build Properties", then after performing a build review the value of "runtime.platform.path".

The downside to platform.local.txt is that when the board package is updated, the file will be lost and needs to be added back in again.
  
Back to top
IP Logged
 
RichardK
Junior Member
**
Offline


Posts: 62
Joined: Jul 11th, 2023
Re: Exceptions for new if no allocation takes place
Reply #14 - Jan 15th, 2025 at 11:58pm
Print Post  
Do I understand you right:

I do not have to set the flags in the boards.txt that I get with add boards.txt using vMicro|Add Code|Add Local board.txt? I already did, but it did not work.

Instead, in 

"%LOCALAPPDATA%\Arduino15\packages\rp2040\hardware\rp2040\4.0.2\platform.txt" 

I have to change the line 
  build.flags.exceptions=-fno-exceptions
to
  build.flags.exceptions=-fexceptions
?

Thanks
Richard
  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2564
Joined: Feb 13th, 2019
Re: Exceptions for new if no allocation takes place
Reply #15 - Jan 16th, 2025 at 9:35am
Print Post  
You can edit the platform.txt directly, or add a seperate platform.local.txt next to it (so you can isolate your changes into a single file).

This will perform the same changes as the local board.txt did in the project (after a VS Restart), but for all consumers of the package.

Apologies for the bad example of the RP2040, there is a menu item "C++ Exceptions" which controls the behaviour for that board package.

The STM32 would need the patterns amending (and copying to platform.local.txt if using that), anywhere that -fno-exceptions appears (and any other patterns you want to enable it for e.g. compiler.c.flags for C code).
  
Back to top
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2564
Joined: Feb 13th, 2019
Re: Exceptions for new if no allocation takes place
Reply #16 - Jan 16th, 2025 at 11:32am
Print Post  
I'm not sure about the syntax to use in boards.txt. 

RP2350
In the boards.txt file created by
Quote:
vMicro|Add Code|Add Local Board.txt

I entered the line
Quote:
build.flags.exceptions=-fexceptions

However, this seems to have no effect. The output of my too_much_push_backs() is the same as before.

The STM32 Issues have been split onto the below thread:
Off-Topic replies have been moved to this Topic.
« Last Edit: Jan 16th, 2025 at 11:33am by Simon@Visual Micro »  
Back to top
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2564
Joined: Feb 13th, 2019
Re: Exceptions for new if no allocation takes place
Reply #17 - Jan 16th, 2025 at 11:34am
Print Post  
For the RP2350 there is no need for any manual overrides, as the menu option does this for you (shown in the below image).  Once selected if you build the project again with vMicro > Compiler > Verbose enabled, you will see the -fexceptions flags in the compiler commands.

Whether this has the desired effect on the vector memory overflow is best reported to the board package author, and as stated previously for the RP2350 I believe it could be related to the PicoSDK in use in that board package, which Visual Micro has no control over.

Using a Hardware Debugger is the best way to determine the root cause of an exception which causes a hard failure in Micro Controllers, and for the RP2350 there are a few options available:
https://www.visualmicro.com/page/RasPiPico-Debugging.aspx

The overrides needed may differ for each board package, and version so there is no one-size-fits-all solution to compiler flags across architectures/time.
« Last Edit: Jan 16th, 2025 at 11:50am by Simon@Visual Micro »  

Please Register or Login to the Forum to see File Attachments
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint