Discussion:
Bad pointer on CString
(too old to reply)
Alexh
2009-09-21 02:19:08 UTC
Permalink
Hi all,

Using Vista SP2, Visual Studio 2005 - all of a sudden some code that
has been working forever and not touched starting giving me a bad
pointer on a CString. It couldn't be simpler -

class Myclass : public CObject
{

CString MyCstring;

...

The debugger reports that the CString has a bad pointer (all 0's).

I tried rebooting, cleaning and rebuilding the entire project. Is it
possible something is corrupted and needs to be deleted?

Thanks
Ulrich Eckhardt
2009-09-21 06:52:04 UTC
Permalink
Post by Alexh
Using Vista SP2, Visual Studio 2005 - all of a sudden some code that
has been working forever and not touched starting giving me a bad
pointer on a CString. It couldn't be simpler -
class Myclass : public CObject
{
CString MyCstring;
...
The debugger reports that the CString has a bad pointer (all 0's).
Looks like an object that is either not fully constructed or already
destroyed. Impossible to tell without knowing much more about what you are
doing.

I suggest you revert back to the commit that caused the error to appear and
from there on try to distill a minimal example which you can post here.
Well, unless of course you found the mistake yourself on the way.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Scot T Brennecke
2009-09-21 07:11:01 UTC
Permalink
Post by Alexh
Hi all,
Using Vista SP2, Visual Studio 2005 - all of a sudden some code that
has been working forever and not touched starting giving me a bad
pointer on a CString. It couldn't be simpler -
class Myclass : public CObject
{
CString MyCstring;
...
The debugger reports that the CString has a bad pointer (all 0's).
I tried rebooting, cleaning and rebuilding the entire project. Is it
possible something is corrupted and needs to be deleted?
Thanks
You clipped out some code, of course. My guess is that was has happened is that some other code has overwritten the object that is
right next to the CString on the stack, thereby corrupting the internal pointer in the CString object.
Alexh
2009-09-21 15:14:00 UTC
Permalink
Post by Alexh
Hi all,
Using Vista SP2, Visual Studio 2005 - all of a sudden some code that
has been working forever and not touched starting giving me a bad
pointer on a CString. It couldn't be simpler -
class Myclass : public CObject
{
CString MyCstring;
...
The debugger reports that the CString has a bad pointer (all 0's).
I tried rebooting, cleaning and rebuilding the entire project. Is it
possible something is corrupted and needs to be deleted?
Thanks
You clipped out some code, of course.  My guess is that was has happened is that some other code has overwritten the object that is
right next to the CString on the stack, thereby corrupting the internal pointer in the CString object.- Hide quoted text -
- Show quoted text -
Thanks guys,

I took the easy way out and restored a backup.
David Webber
2009-09-21 15:26:46 UTC
Permalink
Post by Alexh
class Myclass : public CObject
{
CString MyCstring;
...
The debugger reports that the CString has a bad pointer (all 0's).
*When* is it reporting it?

It isn't at all clear to me that the internal pointer of a CString cannot
legally be NULL. I've always thought it could be, and that the CString
methods allowed for it.

I can't remember if I ever checked, but even if I did, it may be different
in different implementations. But I have always allowed for the
possibility that the standard cast

(LPCTSTR)my_cstring;

might return NULL. If it can, and you assume it doesn't, then you might
run into trouble. But (if I'm right) I can't think where else the
debugger might report a problem?

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Scot T Brennecke
2009-09-22 07:29:59 UTC
Permalink
Post by David Webber
Post by Alexh
class Myclass : public CObject
{
CString MyCstring;
...
The debugger reports that the CString has a bad pointer (all 0's).
*When* is it reporting it?
It isn't at all clear to me that the internal pointer of a CString
cannot legally be NULL. I've always thought it could be, and that the
CString methods allowed for it.
I can't remember if I ever checked, but even if I did, it may be
different in different implementations. But I have always allowed for
the possibility that the standard cast
(LPCTSTR)my_cstring;
might return NULL. If it can, and you assume it doesn't, then you
might run into trouble. But (if I'm right) I can't think where else
the debugger might report a problem?
Dave
Nope. Check out the code for the ATL::CStringT (really the ATL::CSimpleStringT) class templates. You'll see that the m_pszData
member should never be NULL. The (LPCTSTR) cast returns the m_pszData as a string pointer, but it's also expected to be a valid
address that can be manipulated to get to the CStringData object for that string.
David Webber
2009-09-22 22:30:18 UTC
Permalink
Post by Scot T Brennecke
Post by David Webber
It isn't at all clear to me that the internal pointer of a CString cannot
legally be NULL. I've always thought it could be, and that the CString
methods allowed for it.
I can't remember if I ever checked, but even if I did, it may be
different in different implementations. But I have always allowed for
the possibility that the standard cast
(LPCTSTR)my_cstring;
might return NULL. If it can, and you assume it doesn't, then you might
run into trouble. But (if I'm right) I can't think where else the
debugger might report a problem?
Dave
Nope. Check out the code for the ATL::CStringT (really the
ATL::CSimpleStringT) class templates. You'll see that the m_pszData
member should never be NULL. The (LPCTSTR) cast returns the m_pszData as
a string pointer, but it's also expected to be a valid address that can be
manipulated to get to the CStringData object for that string.
Interesting. I must admit my first reaction is one of surprise.

But I guess these days CString is a lot more than just a wrapper around a
(TCHAR *) pointer, so maybe it makes sense in the implementation, not to
allow the data pointer to be NULL.

(Even so, I'll continue to allow for the possibility NULL when I use a cast
to LPCTSTR - I feel safer that way.) :-)

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Martin T.
2009-09-23 13:02:07 UTC
Permalink
Post by David Webber
Post by Scot T Brennecke
Post by David Webber
It isn't at all clear to me that the internal pointer of a CString
cannot legally be NULL. I've always thought it could be, and that
the CString methods allowed for it.
I can't remember if I ever checked, but even if I did, it may be
different in different implementations. But I have always allowed
for the possibility that the standard cast
(LPCTSTR)my_cstring;
might return NULL. If it can, and you assume it doesn't, then you
might run into trouble. But (if I'm right) I can't think where
else the debugger might report a problem?
Dave
Nope. Check out the code for the ATL::CStringT (really the
ATL::CSimpleStringT) class templates. You'll see that the m_pszData
member should never be NULL. The (LPCTSTR) cast returns the m_pszData
as a string pointer, but it's also expected to be a valid address that
can be manipulated to get to the CStringData object for that string.
Interesting. I must admit my first reaction is one of surprise.
But I guess these days CString is a lot more than just a wrapper around
a (TCHAR *) pointer, so maybe it makes sense in the implementation, not
to allow the data pointer to be NULL.
MS implemented that ages ago. (before 1996 ?)
The main reason as far as I can tell is that people used and continued
to use CString Objects in printf and CString::Format(...)
The only way to make this work is making a CString object binary
compatible with a TCHAR pointer and also ensure that this pointer always
points to a valid string, since there is no such thing as a
NULL-string-CString object.
Post by David Webber
(Even so, I'll continue to allow for the possibility NULL when I use a
cast to LPCTSTR - I feel safer that way.) :-)
Weeeeell - A CString object (just as std::string, std::wstring or any
other "normal" string class) should *never* yield a NULL pointer when
asked for its string. So the check is kind of unnecessary.

br,
Martin
Mateusz Loskot
2009-09-26 17:04:58 UTC
Permalink
Post by Alexh
Hi all,
Using Vista SP2, Visual Studio 2005 - all of a sudden some code that
has been working forever and not touched starting giving me a bad
pointer on a CString. It couldn't be simpler -
class Myclass : public CObject
{
CString MyCstring;
<smile>

Perhaps, you have something like this somewhere in your code:

delete MyCstring;

</smile>

(it compiles! it executes!)

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org
Continue reading on narkive:
Loading...