I am encountering a very strange error using freeRTOS on an Arduino Mega2560:

In the setup block I can create an instance of my class:

MyClass* myClass = new MyClass(/*char*/ a, /*char*/ b, new Vector(42));
// new Vector(42) creates a new vector and pushes 42 in as first item

Using a print() method in this class, I get a legitimate output on the console, showing me all the chars and values in the vector.

Using the exact same line inside a task, causes the object not to be created properly. It seems like the fields are empty or there is something entirely wrong (like strange memory errors) with the instance. Printing it again results in an empty line (program doesn't crash) while printing a globally created instance gives the correct result.

Is there any trap in freeRTOS concerning the creation of new instances of an object inside a task? Below is the code for the task creation, increasing the task stack size doesn't help. For debug reasons this the only task running at the moment.

    configMINIMAL_STACK_SIZE + 300,
    tskIDLE_PRIORITY + 1,

1 Answer 1


'Strange memory errors' you seem to be describing values that have gone out of scope.

This is all based on your code, so I may have misunderstood.

The third parameter is causing a memory leak, unless you happen to be deleting it within MyClass (which is normally, not always, bad practice).

It sounds to me that you are trying to access values that have gone out of scope.

Its also possible that this OS requires you to do something to allow the memory to be shared between the tasks.

  • 2
    As I discovered just minutes ago, the FreeRTOS_AVR port only supports C and not C++. This might explain the error when creating a instance with the C++ "new" operator
    – mxcd
    Commented Sep 1, 2016 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.