Some problem with vTaskDelete()

I am trying to learn how to use FreeRTOS , and i encountered some difficulties ; I creat 2 task in my code , one task is led flash and printf “hello world” ; the other is key detect , it is used to detect whether the key is pressed , if the key is pressed , this task will delete the led task; but i find that , if these two task have same priority ,when delete the led task , the program will get stuck , it will trigger configAssert() in heap_4.c , the iamge bellow show the poisition .


but if they have different priority , the program will run normally ; So I’m curious why this happens ; my code is simple , please help me , thanks !

#define START_TASK_PRIO 1
#define START_STK_SIZE  128
TaskHandle_t StartTask_Handler;
void start_task(void *pvParameters);

#define LED0_TASK_PRIO 2
#define LED0_STK_SIZE 50
TaskHandle_t LED0Task_Handler;
void led0_task(void *p_arg);

#define KEY_TASK_PRIO 2
#define KEY_STK_SIZE 50
TaskHandle_t KEY0Task_Handler;
void key0_task(void *p_arg);



int main(void)
{ 
	NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );

	uart_init(115200);
	
	LED_Init();		        
	KEY_Init();				
	
	
	xTaskCreate( (TaskFunction_t)  start_task,
								(const char * )  "start_task",
								(uint16_t)   START_STK_SIZE,
								(void*) NULL,
								( UBaseType_t )START_TASK_PRIO,
								(TaskHandle_t*)&StartTask_Handler	);
	vTaskStartScheduler();
}

void start_task(void *pvParameters )
{
	taskENTER_CRITICAL();
	
		xTaskCreate((TaskFunction_t) key0_task,
				(const char * ) "key0_task",
				(uint16_t) KEY_STK_SIZE,
				(void*) NULL,
				(UBaseType_t) KEY_TASK_PRIO ,
				(TaskHandle_t*)&KEY0Task_Handler
	);
	
	
	
	xTaskCreate ((TaskFunction_t)  led0_task,
								(const char * )  "led0_task",
								(uint16_t)   LED0_STK_SIZE,
								(void*) NULL,
								( UBaseType_t )LED0_TASK_PRIO,
								(TaskHandle_t*)&LED0Task_Handler	);





	vTaskDelete(StartTask_Handler);
	taskEXIT_CRITICAL();
}

void led0_task(void *p_arg)
{
	while(1)
	{
		LED0 = ~LED0;
		vTaskDelay(1000);
		printf("hello World ");
	}
}

void key0_task(void *p_arg)
{
	u8 keyValue;
	while(1)
	{
		keyValue = KEY_Scan(0);
		if(keyValue == KEY0_PRES)
		{	
						printf("Delete LE0_TASK!");
			vTaskDelete(LED0Task_Handler);

		}
	vTaskDelay(10);
		LED1 = ~ LED1;
	}
}

could it be that your code attempts to delete the task twice? I suggest clearing the task handle after the first delete and calling vTaskDelete() only if the task handle is != 0.

You are deleting the start_task before calling taskEXIT_CRITICAL - you will never exit critical section here as taskEXIT_CRITICAL will never execute. Why do you want a critical section here? If you just want to ensure that both led task and key task do not start running before start task is deleted, you can achieve that by just keeping the start task’s priority higher than the other two. Even better would be to create these 2 tasks directly in main and remove the start task all together.

Stack sizes for tasks seem pretty small given that you are using printfs. I’d suggest to increase them to 512 or more.

Lastly, as @RAc pointed out, you need to ensure that you are not attempting to delete an already deleted task -

void key0_task(void *p_arg)
{
	u8 keyValue;
	while(1)
	{
		keyValue = KEY_Scan(0);
		if( (keyValue == KEY0_PRES) && (LED0Task_Handler != NULL) )
		{	
			printf("Delete LE0_TASK!");
			vTaskDelete(LED0Task_Handler);
			LED0Task_Handler = NULL;
		}
	vTaskDelay(10);
		LED1 = ~ LED1;
	}
}
  thanks for your reply ,  i am very greatful for helping me modify my code ;   i  have tried your advice ,and it is useful ! the program is runing as my expected;   the major reason of my problem is stack size is too small .when i increase them, the program can running . i also accept your other advice  ( i didn't notice those fault  before) ,  and  modify my code  .   
thank you !   i am trying to be better  !

thank you for your replay , i accept your advice , i didn’t notice this fault ,thank you!