How do you test for invalid pointers in ATL 
Author Message
 How do you test for invalid pointers in ATL

Hi everyone:

I am trying to find out how to do something like AfxIsInvalidAddress (in
MFC) but in ATL.
is there something outside of MFC that can do that?
Any ideas?

Thanks in advance



Fri, 17 May 2002 03:00:00 GMT  
 How do you test for invalid pointers in ATL
I check for invalid pointers (say, in property implementations) as follows:

HRESULT CMyObject::MyProperty(BSTR *pVal)
{
    // obvious bad pointer
    if(pVal == NULL)
        return E_POINTER;

    try
    {
        // try to assign to the dereferenced pointer
        *pVal = m_bstrSomePrivateDataMember;

        // if no exception, pointer must've been fine
        return S_OK;
    }
    catch(...)
    {
        // hmm...must have been a bad pointer after all, even though it
wasn't NULL
        return E_POINTER;
    }

Quote:
}

This should work for most cases. Although this is specific to COM property
"get" methods, you can see the concepts...

greg


Quote:
>Hi everyone:

>I am trying to find out how to do something like AfxIsInvalidAddress (in
>MFC) but in ATL.
>is there something outside of MFC that can do that?
>Any ideas?

>Thanks in advance



Fri, 17 May 2002 03:00:00 GMT  
 How do you test for invalid pointers in ATL
-----BEGIN PGP SIGNED MESSAGE-----

Quote:
> I am trying to find out how to do something like AfxIsInvalidAddress (in
> MFC) but in ATL.

Win32 has a myriad of memory test functions.

Look up:

IsBadCodePtr
IsBadReadPtr
IsBadStringPtr
IsBadWritePtr

Or you could just rely on exceptions, if you have them enabled.


|                               |     http://www.aynrand.org/       |
| Living a rational, happy life | Try my CD Player for Win9x/NT/2K: |
| of reason and atheism         |     http://www.supercdplayer.com/ |

-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.1

iQCVAwUBOEKhgXoXhLE7VwZzAQGU0AP/RZpDJmkqieDlv0dkur7nBDadNUQKMBvy
uHvRvtsG0Yfmk0dH7Zs8ZiG57p9I4Q9/P5fBlMqx3vkbV3bSdhPTiaorkOUShN4s
rIYunRuXPc5/GD8HxX1OBDGuqKWQHRg9sokFGI1ZTBuVdWwmE4Ez4VOLYFqgn5Tq
Cqa0dswBkko=
=8UHR
-----END PGP SIGNATURE-----



Fri, 17 May 2002 03:00:00 GMT  
 How do you test for invalid pointers in ATL
Brad has the right answer!  These are the right functions to be using.
They're implemented using Win32 exceptions, so there's no C++ exception
overhead.  I like to wrap Read/Write like this:

template <class Q>
verify_read_ptr (Q* q, Q* q2=NULL)
{
#ifdef DEBUG
    ASSERT (!IsBadReadPtr (q, sizeof (Q)));
#endif

Quote:
}

You use it like this:

    MyFunc (CFoo* pUntrusted)
    {
        verify_read_ptr (pUntrusted);
    }

The extra default parameter is to work around a minor bug in the VC compiler
where it has difficulty descerning implied template arguments.  There's a KB
article somewhere. . .


Quote:
> -----BEGIN PGP SIGNED MESSAGE-----

> > I am trying to find out how to do something like AfxIsInvalidAddress (in
> > MFC) but in ATL.

> Win32 has a myriad of memory test functions.

> Look up:

> IsBadCodePtr
> IsBadReadPtr
> IsBadStringPtr
> IsBadWritePtr

> Or you could just rely on exceptions, if you have them enabled.


> |                               |     http://www.aynrand.org/       |
> | Living a rational, happy life | Try my CD Player for Win9x/NT/2K: |
> | of reason and atheism         |     http://www.supercdplayer.com/ |

> -----BEGIN PGP SIGNATURE-----
> Version: PGP 6.5.1

> iQCVAwUBOEKhgXoXhLE7VwZzAQGU0AP/RZpDJmkqieDlv0dkur7nBDadNUQKMBvy
> uHvRvtsG0Yfmk0dH7Zs8ZiG57p9I4Q9/P5fBlMqx3vkbV3bSdhPTiaorkOUShN4s
> rIYunRuXPc5/GD8HxX1OBDGuqKWQHRg9sokFGI1ZTBuVdWwmE4Ez4VOLYFqgn5Tq
> Cqa0dswBkko=
> =8UHR
> -----END PGP SIGNATURE-----



Sat, 18 May 2002 03:00:00 GMT  
 How do you test for invalid pointers in ATL
Thanks everyone


Quote:
> -----BEGIN PGP SIGNED MESSAGE-----

> > I am trying to find out how to do something like AfxIsInvalidAddress (in
> > MFC) but in ATL.

> Win32 has a myriad of memory test functions.

> Look up:

> IsBadCodePtr
> IsBadReadPtr
> IsBadStringPtr
> IsBadWritePtr

> Or you could just rely on exceptions, if you have them enabled.


> |                               |     http://www.aynrand.org/       |
> | Living a rational, happy life | Try my CD Player for Win9x/NT/2K: |
> | of reason and atheism         |     http://www.supercdplayer.com/ |

> -----BEGIN PGP SIGNATURE-----
> Version: PGP 6.5.1

> iQCVAwUBOEKhgXoXhLE7VwZzAQGU0AP/RZpDJmkqieDlv0dkur7nBDadNUQKMBvy
> uHvRvtsG0Yfmk0dH7Zs8ZiG57p9I4Q9/P5fBlMqx3vkbV3bSdhPTiaorkOUShN4s
> rIYunRuXPc5/GD8HxX1OBDGuqKWQHRg9sokFGI1ZTBuVdWwmE4Ez4VOLYFqgn5Tq
> Cqa0dswBkko=
> =8UHR
> -----END PGP SIGNATURE-----



Sat, 18 May 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How do you test for invalid pointers in ATL

2. How do you test for invalid pointers in ATL

3. invalid file or disk full @ 244Mb free space on building tested sample code

4. null/invalid object test

5. test test test test

6. the invalid data of the param passed to createthread function in atl framework

7. ATL/MFC in an extension DLL, invalid afxCurrentResourceHandle

8. Doing relative paths in ATL Server?!?

9. Simpler way of doing things in VC7 ATL?

10. ATL and doing things the wrong way

11. returning an invalid pointer

12. any invalid pointer use?

 

 
Powered by phpBB® Forum Software