Fleet provisioning and flash memory problems

Hey everyone,
We are having some problems with the AWS fleet provisioning and writing to FLASH memory.
We are using the STM32 F767 communicating with AWS through MQTT.
After subscribing to the necessary topics, and registering our “thing”.
When we get the required “accepted” message from the AWS with the certificate, the subscribe callback is called.
In the callback we write the certificate that we got from AWS to the flash memory, and then we get a HARD FAULT.
We already tested the flash writing operation and it works just fine in other uses.
Also, for our logger we are using printf function that prints through UART to the screen. After we write the certificate to the flash, we get some wierd printing like question marks which look like a memory leak or something.

Are there any ideas on what could cause that? maybe the above flow is not correct?
If not, are there any other suggestions rather than writing the certificate to the flash memory(save it in other place)?

Thank you

Did you define configASSERT and enabled stack checking ? Seems there is a memory corruption issue somewhere which might not occur when testing parts of the complete functionality.

In addition to what hs2@ suggested, you can also find out the faulting instruction which can provide some hint - Debugging and diagnosing hard faults on ARM Cortex-M CPUs

Were your test also from FreeRTOS tasks? How long does the flash write operation take, and can you accept interrupts while the flash write is in progress?