PDA

View Full Version : Assigned()



tux
02-09-2005, 03:32 PM
from the delphi help


Assigned can't detect a dangling pointer--that is, one that isn't nil but no longer points to valid data. For example, in the code example for Assigned, Assigned won't detect the fact that P isn't valid.

sample code from delphi help


var P: Pointer;

begin
P := nil;
if Assigned (P) then Writeln ('You won''t see this');
GetMem(P, 1024); {P valid}
FreeMem(P, 1024); {P no longer valid and still not nil}

if Assigned (P) then Writeln ('You''ll see this');
end;


how would i check if P points to valid memory? and would it work to check if it points to a class i need?



[edit]

1 way to do it is to check if a pointer inside the class is assigned and catch any access violation. but thats nasty and no good if running through the ide :(

cairnswm
02-09-2005, 06:24 PM
Whenever you 'clear' a pointer set it to nil. So after FreeMem set the pointer to nil. (Or create a MyFreeMem that calls freemem and then sets it to nil).

For objects always use FreeAnNil(X) rather than X.Free

tux
02-09-2005, 06:27 PM
i cant do that. a function is beeing passed an integer, that is the address of the class.

i want to check the address is valid. at the moment im having to cycle through a TList and search for it but i think it can be more optimized then that (this function is beeing called a *lot*)

cairnswm
03-09-2005, 06:49 AM
Put the pointer in a hash array. The integer then refers to a point in the hash array. The pointer associated with the integer can then be freed and nilled.

You'd need to give us a better code snippet to better understand.

PS. Why not use a pointer instead of an integer. Then you dont need to do the lookup.

tux
03-09-2005, 10:27 AM
we chose integers because its for a dll.


function IsWorldValid(aWorld: Integer): Boolean;
Begin
Result := GPhysicsWorlds.IndexOf(Pointer(aWorld)) > -1;
end;


function IsBodyValid(aWorld, aBody: Integer): Boolean;
Begin
Result := False;

if aWorld <> -1 then
if not IsWorldValid&#40;aWorld&#41; then
Exit;

Result &#58;= TRPWorld&#40;aWorld&#41;.Bodies.IndexOf&#40;Pointer&#40;aBody&#41;&#41; > -1;
end;


this is to try speedup the IsBodyValid, or at least remove the IndexOf()

Paulius
03-09-2005, 06:23 PM
you can use nil, nil is 0. The adress could be farther than maximum integer value, why use integer instead of cardinal?

tux
03-09-2005, 06:27 PM
i know nil is 0. all i want to know is if its possible to find out if a class exists at a memory address without having to access the class and catch any access violation

Paulius
03-09-2005, 07:19 PM
There?¢_Ts nothing else to do except what you?¢_Tre already doing, just maybe optimize it with a hash table, but why would you pass around non valid addresses anyway?

tux
03-09-2005, 07:23 PM
There?¢_Ts nothing else to do except what you?¢_Tre already doing, just maybe optimize it with a hash table, but why would you pass around non valid addresses anyway?


*I* wouldnt pass a non valid address, but its part of an api so i need to check for these things