Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

uC/OS-II and malloc

I am not sure if this is the correct forum, but here goes..

We have an application which uses a couple of threads, most threads are just dormant. In high priority threads that run often, we've tried to minimize the amount of "new" and "delete" utilized to keep performance up. The application runs on NIOS-II on a Cyclone FPGA.

But on several occations, we've seen what looks like a thread issue in "new". It appears that new() can return the same pointer to two threads if the system is stressed. Naturally one of the threads will call delete() on the pointer before the other thread, and so one thread is working with a dangling pointer. This leads to all kinds of mysterious crashes, which occur seemingly at random.

We do not new() or delete () anything in ISRs !!

I guess the question here is : Is new() and delete() thread safe? Are there switches I need to set when compiling?

I don't think this is an issue in uC/OS-II but rather a problem in the standard libs, but perhaps some of you uC/OS-II gurus have seen this sort of thing?

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    There was a thread-safety problem with malloc() (which underlies new), but that was fixed in the 5.0 realease. What version of the Nios II kit are you using?

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    How to complete in version 5.0? What should I do to avoid such problems

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi:

    Try disabling the interrupts (uC/OS-II scheduler uses the system timer interrupt for preemptive context switch), then malloc/free, and then re-enable interrupts.