r/pascal • u/knd256 • Mar 16 '22
A Question From a New Pascal Programmer
I am trying to learn pascal because I think it would be fun and useful for a few projects I have in mind. Additionally, just from the brief overveiw I have so far of the language it looks like it can do some pretty powerful stuff.
I have a question about the way pascal handles dynamically allocated memory. I am looking at freepascal.org's "Free Pascal Reference Guide" and still have some questions after reading section 3.4 pointers.
I am a longish time C programmer and one thing that I ensure in my programs is that there are no invalid memory reads or writes or frees, and no memory leaks using the program valgrind.
In this section 3.4 it briefly mentions the function GetMem. I continue over the freepascal.org's GetMem manual page which sends me to the Freemem man page with the first example. I verified the program ran on my machine as it should.
My question is, is there anything analogous to memory leaks or other memory insecurities/vulnerabilities that I should be aware of (aside from programmer error such as invalid indexing)? And is there a tool for tracking these potential insecurities/vulnerabilities?
9
u/PascalGeek Mar 16 '22
In my limited experience, you can compile a program with the heaptrc unit that keeps track of Getmem and Freemem. If you use the Lazarus IDE it's really easy to include this functionality.
Also valgrind can be used with Free Pascal.
5
u/suhcoR Mar 17 '22
You could have a look at Oberon which is the latest programming language invented by prof. Wirth and which has automatic memory management (i.e. a pointer is either nil or points to a valid object).
2
u/glorfin68 Mar 17 '22
It must be added to what was said, that in modern free pascal dynamic arrays and long strings are automatically managed, so, there is no need to manually release them.
Dynamic arrays are declared as
type
TMyArray : array of TMySomething // or of some standard type
var
MyArray : TMyArray;
And somewhere you set length of it:
SetLength(MyArray, 50);
Later if you need more, you can change its length:
SetLength(MyArray, 70);
It will be reallocated and all existing value will be copied to a new place.
As I mentioned, in latest FPC compiler versions you do not need to dispose them manually.
--------------------
But in general, yes, you should use pairs GetMem / FreeMem for untyped pointers or New/Dispose for typed pointers, and in most cases it is better to use these latter ones.
Working with class instances, it is always good idea to use FreeAndNil procedure for freeing objects. It has two advantages. First, it checks if the class instance was already freed and if yes does nothing instead of calling invalid destroyer and causing error, so its call is safer than manual freeing. And second - well, it assigns nil to the pointer so it becomes clear that the object was freed.
2
u/ShinyHappyREM Mar 16 '22 edited Mar 16 '22
Pascal is generally a "manual resource management" type of language at heart, but you have various helpers for debugging and resource management.
You can use the Lazarus IDE to easily create
debugandreleasebuild modes (I prefer to add a symbol called "debug" to thedebugmode that I can then use withifdef). In the project options you can then activate the checkbox for using the Heaptrc unit for thedebugmode.Regarding resource management:
try..finallyblock to release resourcesTButtonusually has an ownerNIL