Solved

How to validate pointer?

Posted on 2000-03-04
23
300 Views
Last Modified: 2010-04-04
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
Comment
Question by:ITugay
  • 9
  • 7
  • 6
  • +1
23 Comments
 
LVL 1

Expert Comment

by:XAN
ID: 2582764
you may try check
TObject(OBJ).ClassName
0
 
LVL 1

Expert Comment

by:XAN
ID: 2582765
you can try check
TObject(OBJ).ClassName
0
 
LVL 9

Author Comment

by:ITugay
ID: 2582778
To XAN.
It will raise exception if OBJ is invalid.



0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 6

Expert Comment

by:DrDelphi
ID: 2582791
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
 
LVL 1

Expert Comment

by:XAN
ID: 2582794
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
 
LVL 9

Author Comment

by:ITugay
ID: 2582814
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
 
LVL 1

Expert Comment

by:XAN
ID: 2582819
You can turn off Exception handling:
Tools -> Debuger options -> OS Exception
see also
Tools -> Debuger options -> Language Exception -> Stop on Delphi Exceptions
0
 
LVL 9

Author Comment

by:ITugay
ID: 2582821
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
 
LVL 1

Expert Comment

by:XAN
ID: 2582836
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
 
LVL 9

Author Comment

by:ITugay
ID: 2582840


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
 
LVL 9

Author Comment

by:ITugay
ID: 2582844


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
 
LVL 20

Expert Comment

by:Madshi
ID: 2582871
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
 
LVL 1

Expert Comment

by:XAN
ID: 2582894
about Madshi answer
(obj is TObject)
 ==
(obj <> nil)
 ==
(Assigned(obj))
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2582923
No, I think that's different!
0
 
LVL 1

Expert Comment

by:XAN
ID: 2582930
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2582932
Okay, okay, you convinced me...   :-)
0
 
LVL 9

Author Comment

by:ITugay
ID: 2584576
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2584716
I think, you could check "IsBadWritePtr(@YourObj.YourProperty, 4)".
0
 
LVL 1

Accepted Solution

by:
XAN earned 50 total points
ID: 2584809
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2584822
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2584824
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
 
LVL 1

Expert Comment

by:XAN
ID: 2584835
You are right, Madshi ;)
IsObject became better.
but... my method furnish no guarantees... it may be "singular coincidences" :(
0
 
LVL 9

Author Comment

by:ITugay
ID: 2586777
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best Firemonkey component pack 1 119
Communication Between RC4 Delphi <-> PHP 3 124
Adoquery sql  left join does not work 25 99
RESTRequest Parameter 4 43
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question