Solved

How to validate pointer?

Posted on 2000-03-04
23
295 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now