Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic thread memory leakage #8

Closed
jdoe95 opened this issue Nov 26, 2018 · 1 comment
Closed

Dynamic thread memory leakage #8

jdoe95 opened this issue Nov 26, 2018 · 1 comment
Assignees
Labels

Comments

@jdoe95
Copy link
Owner

jdoe95 commented Nov 26, 2018

Memory not released after dynamic thread exits.

Replication:

  • Call os_memory_get_pool_info to obtain pool size: 27468
  • Create a thread using os_thread_create
  • Thread may or may not call os_memory_allocate and os_memory_free
  • Thread exits
  • Call os_memory_get_pool_info again to obtain pool size: 26352
static void user_thread(void)
{
	ushell_printf(TTY_WARN "Dynamic thread started.\n\r" );
	for(unsigned i = 0; i < 10; i++ )
	{
		ushell_printf(TTY_INFO "Hello %u\n\r", i);
		os_thread_delay(100);
	}

	ushell_printf(TTY_WARN "Dynamic thread exiting...\n\r" );
}

void cmd_create_thread( char *argv[], int argc )
{
	(void)argv;
	(void)argc;
	ushell_printf(TTY_INFO "running os_thread_create...\n\r" );
	os_thread_create(2, 1024, user_thread);
}
ushell version 0.2
> 
help
info
lscmd
exit
args
demo
poolinfo
> poolinfo
num blocks in pool 1
pool size 27468

> demo
[INFO]  running os_thread_create...

> [WARN]  Dynamic thread started.
[INFO]  Hello 0
[INFO]  Hello 1
[INFO]  Hello 2
[INFO]  Hello 3
[INFO]  Hello 4
[INFO]  Hello 5
[INFO]  Hello 6
[INFO]  Hello 7
[INFO]  Hello 8
[INFO]  Hello 9
[WARN]  Dynamic thread exiting...

> poolinfo
num blocks in pool 1
pool size 26352

> 

@jdoe95 jdoe95 added the bug label Nov 26, 2018
@jdoe95 jdoe95 self-assigned this Nov 26, 2018
@jdoe95
Copy link
Owner Author

jdoe95 commented Nov 26, 2018

The problem is found here

thd_delete_static( p_thd, &g_sch);

After exiting, dynamic thread jumps to thd_return_hook and then calls os_thread_delete(0) to delete itself.
thd_return_hook
↘️ os_thread_delete(0)
↘️ thd_delete_static() (to remove itself from the ready queue)
↘️ sch_unload_current()

In sch_unload_current() a natural preemption window is opened, and other threads preempts this one immediately. And since current thread was removed from the ready list by thd_delete_static, it never resumes. Memory deallocation only happens after the thread resumes from sch_unload_current()

@jdoe95 jdoe95 closed this as completed in 0ded6f1 Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant