cancel
Showing results for 
Search instead for 
Did you mean: 

HardFault after Constructor of C++ class

dpfeffer9
Associate II
Posted on August 28, 2014 at 13:48

Hi,

I'm facing a problem that I don't understand (using CoIDE 1.7.7 and GCC 4.8 2014q3 for a C++ project) with an STM32F407VGT on the Discovery board. When the first constructor is done and it shall be jumped back to main(), I get thrown to the HardFault-Handler with INVSTATE-bit set in UFSR. The first called Constructor (from main() ) is:

Screen::Screen(uint32_t num_pixels)
{
length = num_pixels;
p_bufferA = new Rgb[length];
p_bufferB = new Rgb[length];
p_calcBuffer = p_bufferA;
p_sendBuffer = p_bufferB;
}

and the the type Rgb is defined as:

union Rgb {
struct {
uint8_t r;
uint8_t g;
uint8_t b;
} ch;
unsigned int data : 24 __attribute__((__packed__));
Rgb();
} 

I need the Union being packed for each being 24 bit. Does anyone have an idea how to solve this problem? Interestingly, I can compile and run the code successfully using an old linker-file, that was automatically generated by CoIDE 1.7.4. This causes around 120 K (!!!) more .data, and I don't know why...
5 REPLIES 5
Posted on August 28, 2014 at 14:39

Perhaps you should review the heap allocation, and ctors initialization?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
dpfeffer9
Associate II
Posted on August 28, 2014 at 15:14

at first: I have to correct myself: .text is growing by 120 k, using the old linker-script. not .data

@clive1: do you mean, that calling malloc(length*sizeof(Rgb)) instead of ''new'' might help? And what is ctors initialization?

Posted on August 28, 2014 at 17:11

Dynamically allocated memory comes from the heap (new, malloc, etc), statically allocated constructors ''ctors'' are typically handled by C/C++ runtime code that initializes things at reset.

Probably Hard Faulting because the allocation/allocator fails.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
dpfeffer9
Associate II
Posted on August 28, 2014 at 21:33

ok, I'll check that. Why would the behaviour change by using another linker script? Do I need special libc for c++? Did it just work before by accident?

Are there any general dos and don'ts for c++ on arm cortex?

Posted on August 28, 2014 at 22:12

I'm not a C++ guy.

You'd have to look at the linker scripts to understand the differences, where specific objects/functions/structures are placed in memory, and if those memories are initialized or not?
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..