Solved

How to validate pointer?

Posted on 2000-03-04
23
299 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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