Calculator Community > Calculator C

OS cleaning up things after program is off?

(1/2) > >>

robly18:
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

Vogtinator:

--- 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?
--- End quote ---
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?
--- End quote ---
YES. While the file is still open, you can not write to or read from it. For example, if you execute this program:
 
--- Code: --- void main(int argc, char **argv) { fopen(argv[0], "rb"); }
--- End code ---
, you can not delete it until you reboot.

robly18:
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?

Vogtinator:

--- Quote ---It's gonna feel weird having to take actual measures to get out of a crash.
--- End quote ---
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?
--- End quote ---
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?
--- End quote ---
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?
--- End quote ---
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

robly18:
Thank you so so so much for your help!


--- Quote from: Vogtinator 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.
--- End quote ---
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.
--- End quote ---

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 from: Vogtinator on September 09, 2015, 03:19:56 pm ---
--- 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?
--- End quote ---
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


--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version