Passing in a void pointer to a task crashes the system? - Simple Code!


The simple test code for ESP32 keeps on crashing, I don’t see what’s wrong?

typedef struct{
   char *print_text;
   uint16_t interval;

void PrintTask(void *arg){
 print_info_t *print_info = (print_info_t *)arg;

   printf("%s", print_info->print_text);

print_info_t *print_task_1;
print_info_t *print_task_2;

void app_main(void)
    print_task_1->print_text = "This is task 1\n";
    print_task_1->interval = 100;
    print_task_2->print_text = "This is task 2\n";
    print_task_2->interval = 500;

    xTaskCreate(PrintTask, "PrintTask1", 4095, (void*)print_task_1, 3, NULL);
    xTaskCreate(PrintTask, "PrintTask2", 4095, (void*)print_task_1, 3, NULL);

This is already invalid because there are no valid objects the pointers point to. Unless of course you do initialize the pointers to valid objects before app_main() is called. Did you mean to write it like this:

print_info_t print_task_1;
print_info_t print_task_2;

and use address operators instead of pointers?

print_task_1 and print_task_2 are pointers that have never been set to point to any structures, so there is no object to actually manipulate.

probably should be:
print_info_t print_task_1;
print_info_t print_task_2;

use print_task_1. instead of →
and pass &print_task_1 to pass the pointer to it (you don’t need the cast, that is implicit)

I did write before app_main()

print_info_t *print_task_1;
print_info_t *print_task_2;

is anything wrong with this?

Ahh, I got it…

But I have set the struct verbal’s in the app_main() ?

You have to define the structs not just pointers to nothing(NULL).

Allocating a pointer to and object does not allocate an object for that pointer to point to.

Beware of placing objects in main, some ports reuse the stack of main for the interrupt stack, so any object there get overwritten. This catches many of the unwary.

Thank You,

I fixed it and it works now!