I am a Windows person, sooo here I go:
Check that you have a valid range of addresses that you can read using IsBadReadPtr(pointer(abitmap), abitmap.InstanceSize)
This can lead to false positives, because
IsBadReadPtr determines its result based upon the mapped virtual pages. However if a type is smaller than a page then it might be freed but the heap manager will still keep a hold of the page if something else is still allocated there. And even if it's all free the heap manager might hold onto the page for performance reasons. Take a look at this:
program tbadptr;
{$mode objfpc}{$H+}
uses
Windows;
var
p1, p2: PLongInt;
begin
New(p1);
New(p2);
Writeln(IsBadReadPtr(p1, SizeOf(LongInt)));
Writeln(IsBadReadPtr(p2, SizeOf(LongInt)));
Dispose(p1);
Writeln(IsBadReadPtr(p1, SizeOf(LongInt)));
Writeln(IsBadReadPtr(p2, SizeOf(LongInt)));
Dispose(p2);
Writeln(IsBadReadPtr(p1, SizeOf(LongInt)));
Writeln(IsBadReadPtr(p2, SizeOf(LongInt)));
end.
This will write
FALSE six times.
form1: TForm
is good practice LOL
1) it declares global variable form1 as type TForm
2) the declaration is global so the compiler takes care to make it NIL
if you do the same inside a procedure locals THEN you must set it yourself to nil because the variable does not reside in global mem but onto the stack
ex.
procedure blabla;
var form1:TForm=nil;
...
the var inside a procedure is local to it and the compiler does not init locals
Hum, and what about ( ouch, they will shout ) to propose to always assign to nil if nothing was assigned.
So, even in a local proc, if somebody does:
procedure blabla;
var form1:TForm;
the compiler will takes care to make form1 to NIL too?
Why this difference?
The difference is that global variables are initialized by the operating system when loading the binary (at least in modern OSes), thus those are set to 0, Nil, etc.
for free. For local variables that needs to be done
actively thus it might result in the addition of instructions that are simply not necessary because the variable is assigned a value later on.
Refering to this, I would like to know what is the utility of assigned() if the object must be assigned by something, ( nil for nothing )?
I understood that assigned() is true if the object <> nil.
But if the result is not assigned(), in fact he must be assigned as nil.
So what must be used to know if something was not assigned by something ( even not nil )?
It's very expensive and potentially non-computable to check for "Is not valid", because this would mean to walk the complete heap and other such things and even then one might not catch everything. Thus things like that are simply not done and
Assigned only checks for
Nil or not.