前言
- 自己在使用动态内存过程中,重复rt_free一个指针,发现竟然没有出错!
- 忽然发现自己的代码异常的健壮,不出现RT_ASSERT断言死机?
- 经过软件调试,发现:自己关闭了 RT_ASSERT功能!!
开启RT_ASSERT功能
分析原因
- 要开启:
#define RT_DEBUG
- 为什么不开启:
#define RT_DEBUG
,RT_ASSERT就不起作用了呢?
- 原来RT_ASSERT代码的执行,rt_assert_handler,依赖:RT_DEBUG
/* rt-thread\src\kservice.c */
#ifdef RT_DEBUG
/* RT_ASSERT(EX)'s hook */
void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
/**
* This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
*
* @param hook the hook function
*/
void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
{
rt_assert_hook = hook;
}
/**
* The RT_ASSERT function.
*
* @param ex the assertion condition string
* @param func the function name when assertion.
* @param line the file line number when assertion.
*/
void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
{
volatile char dummy = 0;
if (rt_assert_hook == RT_NULL)
{
#ifdef RT_USING_MODULE
if (dlmodule_self())
{
/* close assertion module */
dlmodule_exit(-1);
}
else
#endif
{
rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
while (dummy == 0);
}
}
else
{
rt_assert_hook(ex_string, func, line);
}
}
RTM_EXPORT(rt_assert_handler);
#endif /* RT_DEBUG */
问题解决
.config - RT-Thread Configuration
RT-Thread Kernel
[*] Enable debugging features --->
- 使能:#define RT_DEBUG后,RT_ASSERT正常了
- 至于什么时候关闭的这个内核选项,不清楚了,遇到问题,多请教,多调试,多总结。