Results 1 to 9 of 9

Thread: Assigned()

  1. #1

    Assigned()

    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

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

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Assigned()

    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
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Assigned()

    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*)

  4. #4
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Assigned()

    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.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  5. #5

    Assigned()

    we chose integers because its for a dll.

    Code:
    function IsWorldValid(aWorld: Integer): Boolean;
    Begin
      Result := GPhysicsWorlds.IndexOf(Pointer(aWorld)) > -1;
    end;
    Code:
    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()

  6. #6

    Assigned()

    you can use nil, nil is 0. The adress could be farther than maximum integer value, why use integer instead of cardinal?

  7. #7

    Assigned()

    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

  8. #8

    Assigned()

    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?

  9. #9

    Assigned()

    Quote Originally Posted by Paulius
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •