• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

How to validate pointer?

I have a pointer, and it's value not "nil", I would like to know: "Is it point to valid TObject? Can I call it's methods?"

I can do it like this:
  ...
  try
    OBJ.SomeMethod;
    // do something here
  except
    // OBJ is invalid
  end;

I would like to do it by another way, without generating exceptions.

Igor.
0
ITugay
Asked:
ITugay
  • 9
  • 7
  • 6
  • +1
1 Solution
 
XANCommented:
you may try check
TObject(OBJ).ClassName
0
 
XANCommented:
you can try check
TObject(OBJ).ClassName
0
 
ITugayAuthor Commented:
To XAN.
It will raise exception if OBJ is invalid.



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DrDelphiCommented:
ITugay,
  Why are you trying to avoid exceptions? They are probably one of the best tools we have at our disposal. In your situation, the exception is the best choice for testing if the pointer is valid. All that need be done is to handle the exception.
0
 
XANCommented:
Yeh, you could not avoid try-except. but you can separatly process exception of your object methods and exception that raised by wrong object pointer.
May be you should review your program concepts?
0
 
ITugayAuthor Commented:
To DrDelphi.

Ok, I know that exception is easy way to validate pointer. But I don't like when it happen under Delphi IDE. It's terminate execution and I need always press [Run] button. In some cases, my application have time critical section, and interrupting of application cause irreversible consequences. Certainly I can run it without IDE, but it complicates debugging the application.

Best regards,

Igor.
0
 
XANCommented:
You can turn off Exception handling:
Tools -> Debuger options -> OS Exception
see also
Tools -> Debuger options -> Language Exception -> Stop on Delphi Exceptions
0
 
ITugayAuthor Commented:
To XAN.

It seems tom me, I need to review my programm. At the beginnig, I think that validating pointer without exception is very easy. I believed that I have missed something in programming:) Now I see, that is realy difficult.

Best regards,
Igor.
0
 
XANCommented:
One suggestion yet
may be you need use not Pointer but TMyObject as base (abstract) class (and TMyClass as "class of TMyObject"). In some cases it is useful. For example you always can check Obj.InheritsFrom(TMyClass). ...
0
 
ITugayAuthor Commented:


I knew about possibility to switch off some exceptions. But it was realy interesting and basic quetion for me to validate pointer without exception. And it seems to me, now I have an answer.

Thanx for help.

PS: The previous explanation was simply example which has guided me on this question
0
 
ITugayAuthor Commented:


I knew about possibility to switch off some exceptions. But it was realy interesting and basic quetion for me to validate pointer without exception. And it seems to me, now I have an answer.

Thanx for help.

PS: The previous explanation was simply example which has guided me on this question
0
 
MadshiCommented:
You could use this

  try
    if (not IsBadWritePtr(obj, 4)) and
       (obj is TObject) then
      obj.UseAMethod;
  except ... end;

This should catch all exceptions (not in the IDE of course), but it should also throw out exceptions less often.

Regards, Madshi.
0
 
XANCommented:
about Madshi answer
(obj is TObject)
 ==
(obj <> nil)
 ==
(Assigned(obj))
0
 
MadshiCommented:
No, I think that's different!
0
 
XANCommented:
Madshi,
if we look at CPU View
  obj is TObject
is
  test eax, eax
  ret
that is only if eax (=obj) is null we can retriev False
0
 
MadshiCommented:
Okay, okay, you convinced me...   :-)
0
 
ITugayAuthor Commented:
OK, thanx to XAN and Madshi.

It's good idea to check memory before read some address. I need only find out address in memory, where some property of OBJ stored. Then check possibility to read this addres. Reading this property will get me information about object. (I can to assign to this property some unical value in constructor, and clear value in destructor). If data in memory have my unical value and is readable, then I call methods of OBJ, is it?

Any comments?

PS: I'm ready accept comments from Madshi as answer. I mean "IsBadWritePtr". But it will close the question. Does anybody have objections?




0
 
MadshiCommented:
I think, you could check "IsBadWritePtr(@YourObj.YourProperty, 4)".
0
 
XANCommented:
My friends!
I know, I know, I know the secret way ;) You can play with the next code

  function _IsObject(Ptr: Pointer): Boolean;
  begin
    asm
      mov ebx, [eax]
      mov eax, [ebx+vmtSelfPtr]
      cmp eax, ebx
      jne @wrong
      mov al, 1
      ret
    @wrong:
      xor eax, eax
      ret
    end;
  end;

  function IsObject(Ptr: Pointer): Boolean;
  begin
    try
      Result := _IsObject(Ptr);
    except
      Result := False
    end;
  end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if IsObject(Self) then
    ShowMessage('Yes !-)')
  else
    ShowMessage('8( Starnge...');
  if IsObject(Pointer(123456)) then
    ShowMessage('Yes !-)')
  else
    ShowMessage('8( Starnge...');
end;

I hope it will be useful $)
0
 
MadshiCommented:
My compliment - that's really a good idea! But it will throw out an exception if you give in e.g. "0" or "1". May I suggest a little extension?

  function IsObject(Ptr: Pointer): Boolean;
  begin
    try
      Result := IsBadWritePtr(Ptr, 4) and _IsObject(Ptr);
    except
      Result := False
    end;
  end;

This should be almost perfect!   :-)
0
 
MadshiCommented:
Well, another thought, if we give in an object that is already deallocated, this check function could eventually pass without error. So ITugay, perhaps you can combine this with your idea, namely a property that is filled in the constructor and cleared in the destructor.
0
 
XANCommented:
You are right, Madshi ;)
IsObject became better.
but... my method furnish no guarantees... it may be "singular coincidences" :(
0
 
ITugayAuthor Commented:
Thanx XAN, Madshi.

I hope you enjoy of question;) Compilation of yours ideas will work in my case, I'm sure.
I will grade both of you. At first XAN, then Madshi.
Madshi, you can find out new "to Madshi: validating" question. And put here some comments. I will accept it too.

Best regards,
Igor.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 7
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now