This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

NDK Stack does not free all dynamic memory on close

Hi,

  We are running the NDK stack and we want to shut down the stack under certain conditions.  We have found the calling NC_NetStop(0) does not free all the dynamic memory created by NC_NetStart.  I used the configDemo as a baseline for the stack.  I have opened and closed the stack in pieces to verify all other dynamic memory created is being released. Is there anything else I need to call?

Thanks,

Will

  • I would like to also add that the difference is 304 bytes.  Im looking at the dynamic memory and for some reason I can't de-allocate 304 bytes.

    Will

  • Will,

    I searched and didn’t find any known issues like this.  

    What version of NDK and other software components are you using, and for which device? 

    How are you determining there is a leak?  With ROV? 

    And are you on a TI development board, or a custom board?

    Thanks,
    Scott

  • Hi Scott,

      We are running NDK 3.22.3.20 for the C6748.  I am running SYS/BIOS 6.35.3.47.  I would not say its a memory leak because it only looses the 304 bytes the first time its created then destroyed.  After that, what is created is then destroyed.  What this says to me is that something is checked the first time and if its not created it creates it.  On the second or third time creating the stack, the code sees something is already created so it doesn't need to re-create it.  I see 2 semaphores that are created but not destroyed.  The memory is using the ROV and looking at the HeapMem.  We modified and rebuilt parts of the stack (HTTP, MEM, and TASK) and found these issues.  I downloaded a fresh copy of the NDK 3.22.3.20 and ran the same test with the released libraries.  I have the same issue with the same memory values.  THis way I have ruled out our changes causing this issue.  Any help would be greatly appreciated.

    Will

  • Will,

    Thanks for the additional info.

    I asked around and what you are describing is not expected behavior.  I will try to recreate this and see what is going on.  I will get back to you when I know more…

    Scott

  • Any updates?  Were you able to reproduce the same issues? 

    Thanks,

    Will

  • Will,

    Sorry for the delay.  No, I haven’t reproduced it yet, but hope to get back to this later today.

    Scott

  • Will,

    Sorry, just tonight I had some time to look at this.  :OP

    I’m using an M4 device to try this out.  I do see memory left allocated after NC_NetStart() returns.  After looking into the code, I think this is to be expected, and that this memory will eventually be freed. 

    I still have to consult with an expert on this, and will get back to you ASAP…

    Scott

  • Will,

    Is it possible for you to send your test program that is showing the 304 bytes difference?  I’d like to see exactly where you are measuring before/after memory usage.

    In my particular test on an M4, the memory left over after NC_NetStart() returned was due to the NS_BootTask, that is used to startup the stack, but then terminates itself.  (At the end of its task function, it does a TaskExit() which puts the task in the terminated state.)  But the task is not yet deleted.  That ‘cleanup’ will happen only when the Idle task gets to run later, when terminated tasks will be deleted.  In my test case, there were 0x880 less bytes free after NC_NetStart() returned.  And 0x800 of this was due to the allocated but not yet freed task stack for NS_BootTask().  Of the 0x80 left, 0x4C would correspond to the Task object for NS_BootTask.  The remaining 0x34 not freed is probably due to some additional allocation relating to this boot up task, but I haven’t resolved this completely.

    I -was- able to confirm today that the behavior of this NS_BootTask() being in the terminated state, and not getting deleted until the Idle task runs, is expected.  

    A consequence of this is that if the Idle task doesn’t get to run to do cleanup, before NC_NetStart() is called again, that it will require additional memory to create another NS_BootTask().  And if that keeps happening before the Idle task runs, it might result in an out of memory condition.  I will probably file a bug report for this, but I need to investigate a bit further first.

    So, I’d like to look at your test, to see exactly how you are measuring the difference.  If you can’t post it publicly to the forum, we can become “friends” on the forum, and you can send it to me privately.  Can you please send this?

    Thanks,
    Scott

  • Request sent.

    Will

  • Hi Scott,

     I added a zip to my files.  Im guessing that's how you can see it.  The project falls under application Software.  The platform to use is the AppPlatform.  The only thing you will have to change is the path to your NDK install directory found in the file exMemSections in the Application Software.  Other than that, it should build just fine.  Using my example, 336 bytes are left on the table after the create and then remove of the NDK stack.  2 Semaphores are created but not removed also.  Please let me know what you find.

    THanks,

    Will

  • Hi Will,

    Got the file, thanks for sending it.  

    I’ll get back when I have more info…

    Thanks,
    Scott

  • Hi Scott,

    Any updates?

    Thanks,
    Will
  • Hi Will,

    I've talked with Scott and I'm going to respond once I look into it more.

    Todd

  • Here's what's happening with the "leaked" memory. It is actually 3 different pieces of memory

    1. printf: The first time printf is called, there is some memory allocation. This memory cannot be reclaimed. It looks like printf is being called the first time in your EMAC_getConfig function. You can use System_printf if you do not like this behavior with printf. The kernel team generally not to use printf and use System_printf instead.

    2. llEnter: The stack uses this to enter a critical region (and llExit to leave). The first time this is called, a semaphore is created. It currently cannot be deleted.

    3. DaemonNew: The first time this is called, it creates a semaphore to allow resource management. It currently cannot be deleted.

    Hopefully this clears up where the memory is going.

    Todd