Author Topic: OS cleaning up things after program is off?  (Read 2544 times)

0 Members and 1 Guest are viewing this topic.

Offline robly18

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +0/-0
    • View Profile
OS cleaning up things after program is off?
« on: September 07, 2015, 08:34:01 pm »
So I recently got into TI-Nspire CX programming, and I've had this looming worry... Does the OS deal with cleanup after program exit? i.e. say I malloc some memory and exit without freeing it. Does the OS recognize that it should be freed?

Furthermore, say I open a file and then shut down the program without closing  it. Would there be any problems stemming from that?

TIA

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1192
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: OS cleaning up things after program is off?
« Reply #1 on: September 08, 2015, 06:49:02 am »
Quote
So I recently got into TI-Nspire CX programming, and I've had this looming worry... Does the OS deal with cleanup after program exit? i.e. say I malloc some memory and exit without freeing it. Does the OS recognize that it should be freed?
No, the TI-Nspire system is based on an RTOS, which isn't like linux in such cases. The way that ndless implements program loading is incompatible with other tasks as well (the ploader_hook runs in the docbrowser task with preemption disabled), so every program has to clean up. It is possible to implement such functionality in ndless itself, but it may break other things, like background tasks, hooks, etc.

Quote
Furthermore, say I open a file and then shut down the program without closing  it. Would there be any problems stemming from that?
YES. While the file is still open, you can not write to or read from it. For example, if you execute this program:
 
Code: [Select]
void main(int argc, char **argv) { fopen(argv[0], "rb"); } , you can not delete it until you reboot.

Offline robly18

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +0/-0
    • View Profile
Re: OS cleaning up things after program is off?
« Reply #2 on: September 08, 2015, 09:50:54 am »
Thanks. So, unlike on most computers, I have to make sure all cleanup is done then, got it.

It's gonna feel weird having to take actual measures to get out of a crash.

Also, two extra questions:
1- Is there any risk stemming from pointer shenanigans? Like, say I forget to assign a pointer and write to it. Is there any chance of me permanently messing up my calculator?

2- This one should be more evident but I trust that malloc'd memory is freed upon calculator reset?

EDIT: Bonus third (sorry for so many!), is there any way to track allocated memory so that I may be able to find out if my program has a memory leak?
« Last Edit: September 08, 2015, 09:58:57 am by robly18 »

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1192
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: OS cleaning up things after program is off?
« Reply #3 on: September 09, 2015, 03:19:56 pm »
Quote
It's gonna feel weird having to take actual measures to get out of a crash.
Well, if your program crashes, it's not possible to recover and the calc resets.
abort and std::terminate (C++) are caught and it tries to recover from it, but that's not possible sometimes.

Quote
1- Is there any risk stemming from pointer shenanigans? Like, say I forget to assign a pointer and write to it. Is there any chance of me permanently messing up my calculator?
Unless you touch the nand write functions, you're safe. I heard that under some weird circumstances it's possible to corrupt the OS in such a way that it uninstalls itself, but that's not a major issue anyway.

Quote
2- This one should be more evident but I trust that malloc'd memory is freed upon calculator reset?
Yes. Only the data written to NAND is persistent.

Quote
EDIT: Bonus third (sorry for so many!), is there any way to track allocated memory so that I may be able to find out if my program has a memory leak?
There is no valgrind for ndless, but you can try to modify libsyscalls in the Ndless-SDK a bit to print something on each malloc and free and track it manually over the serial port or have an internal counter: https://github.com/ndless-nspire/Ndless/blob/master/ndless-sdk/libsyscalls/stdlib.cpp#L104

Offline robly18

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +0/-0
    • View Profile
Re: OS cleaning up things after program is off?
« Reply #4 on: September 10, 2015, 08:31:45 am »
Thank you so so so much for your help!

Quote
It's gonna feel weird having to take actual measures to get out of a crash.
Well, if your program crashes, it's not possible to recover and the calc resets.
abort and std::terminate (C++) are caught and it tries to recover from it, but that's not possible sometimes.

Maybe crash isn't the proper word.. I meant as in, failure to read a file and I end up aborting for example. On the computer all I have to do is have the program quit, but here I see myself implementing some ad-hoc version of exceptions using gotos.


Quote
EDIT: Bonus third (sorry for so many!), is there any way to track allocated memory so that I may be able to find out if my program has a memory leak?
There is no valgrind for ndless, but you can try to modify libsyscalls in the Ndless-SDK a bit to print something on each malloc and free and track it manually over the serial port or have an internal counter: https://github.com/ndless-nspire/Ndless/blob/master/ndless-sdk/libsyscalls/stdlib.cpp#L104


How would I go about, say, logging things to a file perhaps? I've tried using the C IO functions, but I found myself unable to write things? It created the file and everything, but failed when I tried to write to it.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1192
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: OS cleaning up things after program is off?
« Reply #5 on: September 14, 2015, 05:21:42 am »
Thank you so so so much for your help!

Quote
It's gonna feel weird having to take actual measures to get out of a crash.
Well, if your program crashes, it's not possible to recover and the calc resets.
abort and std::terminate (C++) are caught and it tries to recover from it, but that's not possible sometimes.

Maybe crash isn't the proper word.. I meant as in, failure to read a file and I end up aborting for example. On the computer all I have to do is have the program quit, but here I see myself implementing some ad-hoc version of exceptions using gotos.
You can just use C++ exceptions, it's cleaner than abort() anyway.

Quote
Quote
EDIT: Bonus third (sorry for so many!), is there any way to track allocated memory so that I may be able to find out if my program has a memory leak?
There is no valgrind for ndless, but you can try to modify libsyscalls in the Ndless-SDK a bit to print something on each malloc and free and track it manually over the serial port or have an internal counter: https://github.com/ndless-nspire/Ndless/blob/master/ndless-sdk/libsyscalls/stdlib.cpp#L104
How would I go about, say, logging things to a file perhaps? I've tried using the C IO functions, but I found myself unable to write things? It created the file and everything, but failed when I tried to write to it.
It should work just fine, can you show us your code?

Offline robly18

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +0/-0
    • View Profile
Re: OS cleaning up things after program is off?
« Reply #6 on: September 22, 2015, 05:48:04 pm »
(Sorry for taking so long... School just started.)

Okay so, after trying again I guess it worked. Don't know why I thought it did the first few tries. The first few, there must have been no output, and after I added error checking, when I mistakenly assumed fprintf would return 0 on success, I came to the conclusion that it had failed...

Thanks for all your help!