CString, char && delete 
Author Message
 CString, char && delete

Hi,
I have a program

void CMyApp::A_Funtion()
{
CString A = "Hello A";
m_struct.A = new char[ A.GetLength() +1 ];
strcpy( m_struct.A, A );
m_struct.A[ A.GetLength() ] = '\0';

CString B = "Hello B";
m_struct.B = new char[ B.GetLength() +1 ];
strcpy( m_struct.B, B );
m_struct.B[ B.GetLength() ] = '\0';

Quote:
}

void CMyApp::MainLoop()
{
    m_struct = new CMyStruct;
    A_Function();
    //....
    // Do some more work

    delete m_struct.A;
    delete m_struct.B;
    delete m_struct;

Quote:
}

I am assuming that my CStrings will never be empty.
Now my problem is that delete B give me an assert, (and a Crash).
Why, am I not doing my 'new' properly?

How should it be done? Is there a better way?

Many thanks
--
FFMG
Please remove _NOSPAM in email address for reply.



Mon, 09 Feb 2004 17:47:48 GMT  
 CString, char && delete
FFMG,
   you're trying to copy a string from a CString object pointer...

Quote:
> strcpy( m_struct.A, A );
   instead of
> strcpy( m_struct.A, (LPCTSTR)A );
> strcpy( m_struct.B, (LPCTSTR)B );

by copying from the object instead of the "string IN the object" you're
probably copying too much since strcpy copies until it reaches a NULL.  My
guess is that between your A copy and B copy you trash something sensative
that shows up when you delete...

HTH
wardo


Quote:
> Hi,
> I have a program

> void CMyApp::A_Funtion()
> {
> CString A = "Hello A";
> m_struct.A = new char[ A.GetLength() +1 ];
> strcpy( m_struct.A, A );
> m_struct.A[ A.GetLength() ] = '\0';

> CString B = "Hello B";
> m_struct.B = new char[ B.GetLength() +1 ];
> strcpy( m_struct.B, B );
> m_struct.B[ B.GetLength() ] = '\0';
> }

> void CMyApp::MainLoop()
> {
>     m_struct = new CMyStruct;
>     A_Function();
>     //....
>     // Do some more work

>     delete m_struct.A;
>     delete m_struct.B;
>     delete m_struct;
> }

> I am assuming that my CStrings will never be empty.
> Now my problem is that delete B give me an assert, (and a Crash).
> Why, am I not doing my 'new' properly?

> How should it be done? Is there a better way?

> Many thanks
> --
> FFMG
> Please remove _NOSPAM in email address for reply.



Tue, 10 Feb 2004 03:24:01 GMT  
 CString, char && delete

Quote:

>Hi,
>I have a program

>void CMyApp::A_Funtion()
>{
>CString A = "Hello A";
>m_struct.A = new char[ A.GetLength() +1 ];
>strcpy( m_struct.A, A );
>m_struct.A[ A.GetLength() ] = '\0';

>CString B = "Hello B";
>m_struct.B = new char[ B.GetLength() +1 ];
>strcpy( m_struct.B, B );
>m_struct.B[ B.GetLength() ] = '\0';
>}

You don't need to zero-terminate above; strcpy has done it for you.

Quote:
>void CMyApp::MainLoop()
>{
>    m_struct = new CMyStruct;
>    A_Function();
>    //....
>    // Do some more work

>    delete m_struct.A;
>    delete m_struct.B;
>    delete m_struct;
>}

>I am assuming that my CStrings will never be empty.
>Now my problem is that delete B give me an assert, (and a Crash).
>Why, am I not doing my 'new' properly?

You must delete what you new and delete[] what you new[]. Your
deletions of A and B are undefined, because you're using delete
instead of the required delete[].

Quote:
>How should it be done? Is there a better way?

Why isn't CMyStruct's destructor responsible for deleting its members
A and B? Also, what happens if the allocation of A succeeds but B
throws an exception?

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Tue, 10 Feb 2004 04:07:05 GMT  
 CString, char && delete

Quote:

>FFMG,
>   you're trying to copy a string from a CString object pointer...
>> strcpy( m_struct.A, A );
>   instead of
>> strcpy( m_struct.A, (LPCTSTR)A );

Due to CString::operatr LPCTSTR, the two are equivalent. You do need
to cast when passing CStrings to printf and other variadic functions,
but strcpy's second parameter is const char*, so the cast is
superfluous for strcpy.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Tue, 10 Feb 2004 04:52:03 GMT  
 CString, char && delete

Quote:
> FFMG,
>    you're trying to copy a string from a CString object pointer...
> > strcpy( m_struct.A, A );
>    instead of
> > strcpy( m_struct.A, (LPCTSTR)A );

Doesn't matter. One of the cool things about CString is its implicit
(LPCTSTR) operator, which lets it act exactly as an LPCTSTR without casting,
anywhere that such a pointer is required (as in the second argument of
strcpy). So the second statement above is precisely equivalent to the first.

--
Jim Johnson
Metaphoric Software
-------------------
Makers of Techno Toys
Software for Electronic Music
http://www.technotoys.com



Tue, 10 Feb 2004 07:57:24 GMT  
 CString, char && delete
Try
    delete [] m_struct->A;
    delete [] m_struct->B;
Quote:

> FFMG,
>    you're trying to copy a string from a CString object pointer...
> > strcpy( m_struct.A, A );
>    instead of
> > strcpy( m_struct.A, (LPCTSTR)A );
> > strcpy( m_struct.B, (LPCTSTR)B );

> by copying from the object instead of the "string IN the object" you're
> probably copying too much since strcpy copies until it reaches a NULL.  My
> guess is that between your A copy and B copy you trash something sensative
> that shows up when you delete...

> HTH
> wardo



> > Hi,
> > I have a program

> > void CMyApp::A_Funtion()
> > {
> > CString A = "Hello A";
> > m_struct.A = new char[ A.GetLength() +1 ];
> > strcpy( m_struct.A, A );
> > m_struct.A[ A.GetLength() ] = '\0';

> > CString B = "Hello B";
> > m_struct.B = new char[ B.GetLength() +1 ];
> > strcpy( m_struct.B, B );
> > m_struct.B[ B.GetLength() ] = '\0';
> > }

> > void CMyApp::MainLoop()
> > {
> >     m_struct = new CMyStruct;
> >     A_Function();
> >     //....
> >     // Do some more work

> >     delete m_struct.A;
> >     delete m_struct.B;
> >     delete m_struct;
> > }

> > I am assuming that my CStrings will never be empty.
> > Now my problem is that delete B give me an assert, (and a Crash).
> > Why, am I not doing my 'new' properly?

> > How should it be done? Is there a better way?

> > Many thanks
> > --
> > FFMG
> > Please remove _NOSPAM in email address for reply.



Tue, 10 Feb 2004 08:58:06 GMT  
 CString, char && delete
Thanks all for the reply,

I just need to clarify.
first,  I am doing
    delete [] m_struct->A;
    delete [] m_struct->B;
not
    delete m_struct.A;
    delete m_struct.B;
That was a very bad typo from my side.

secondly I could put it in the destructor but the fact remains that I cannot
delete it.
It always crashes, (delete [] m_struct->B is crashing).

I don't know if this will help but the Assert message is

File:dbgheap.c
Line:1109

Expression: _pFirstBlock == pHead

I can use m_struct->B without any problems. Even if I put a break just
before to the delete I can still see that the data is valid.
I just can't see why it would be crashing.

--
FFMG
Please remove _NOSPAM in email address for reply.

Quote:
> FFMG,
>    you're trying to copy a string from a CString object pointer...
> > strcpy( m_struct.A, A );
>    instead of
> > strcpy( m_struct.A, (LPCTSTR)A );
> > strcpy( m_struct.B, (LPCTSTR)B );

> by copying from the object instead of the "string IN the object" you're
> probably copying too much since strcpy copies until it reaches a NULL.  My
> guess is that between your A copy and B copy you trash something sensative
> that shows up when you delete...

> HTH
> wardo



> > Hi,
> > I have a program

> > void CMyApp::A_Funtion()
> > {
> > CString A = "Hello A";
> > m_struct.A = new char[ A.GetLength() +1 ];
> > strcpy( m_struct.A, A );
> > m_struct.A[ A.GetLength() ] = '\0';

> > CString B = "Hello B";
> > m_struct.B = new char[ B.GetLength() +1 ];
> > strcpy( m_struct.B, B );
> > m_struct.B[ B.GetLength() ] = '\0';
> > }

> > void CMyApp::MainLoop()
> > {
> >     m_struct = new CMyStruct;
> >     A_Function();
> >     //....
> >     // Do some more work

> >     delete m_struct.A;
> >     delete m_struct.B;
> >     delete m_struct;
> > }

> > I am assuming that my CStrings will never be empty.
> > Now my problem is that delete B give me an assert, (and a Crash).
> > Why, am I not doing my 'new' properly?

> > How should it be done? Is there a better way?

> > Many thanks
> > --
> > FFMG
> > Please remove _NOSPAM in email address for reply.



Tue, 10 Feb 2004 17:35:13 GMT  
 CString, char && delete
Could you republish your code (preferably the actual code), it looks like
you still have typos... In some places you use "m_struct." and others you
use "m_struct->".

Thanks,
John Flegert


Quote:
> Thanks all for the reply,

> I just need to clarify.
> first,  I am doing
>     delete [] m_struct->A;
>     delete [] m_struct->B;
> not
>     delete m_struct.A;
>     delete m_struct.B;
> That was a very bad typo from my side.

> secondly I could put it in the destructor but the fact remains that I
cannot
> delete it.
> It always crashes, (delete [] m_struct->B is crashing).

> I don't know if this will help but the Assert message is

> File:dbgheap.c
> Line:1109

> Expression: _pFirstBlock == pHead

> I can use m_struct->B without any problems. Even if I put a break just
> before to the delete I can still see that the data is valid.
> I just can't see why it would be crashing.

> --
> FFMG
> Please remove _NOSPAM in email address for reply.


> > FFMG,
> >    you're trying to copy a string from a CString object pointer...
> > > strcpy( m_struct.A, A );
> >    instead of
> > > strcpy( m_struct.A, (LPCTSTR)A );
> > > strcpy( m_struct.B, (LPCTSTR)B );

> > by copying from the object instead of the "string IN the object" you're
> > probably copying too much since strcpy copies until it reaches a NULL.
My
> > guess is that between your A copy and B copy you trash something
sensative
> > that shows up when you delete...

> > HTH
> > wardo



> > > Hi,
> > > I have a program

> > > void CMyApp::A_Funtion()
> > > {
> > > CString A = "Hello A";
> > > m_struct.A = new char[ A.GetLength() +1 ];
> > > strcpy( m_struct.A, A );
> > > m_struct.A[ A.GetLength() ] = '\0';

> > > CString B = "Hello B";
> > > m_struct.B = new char[ B.GetLength() +1 ];
> > > strcpy( m_struct.B, B );
> > > m_struct.B[ B.GetLength() ] = '\0';
> > > }

> > > void CMyApp::MainLoop()
> > > {
> > >     m_struct = new CMyStruct;
> > >     A_Function();
> > >     //....
> > >     // Do some more work

> > >     delete m_struct.A;
> > >     delete m_struct.B;
> > >     delete m_struct;
> > > }

> > > I am assuming that my CStrings will never be empty.
> > > Now my problem is that delete B give me an assert, (and a Crash).
> > > Why, am I not doing my 'new' properly?

> > > How should it be done? Is there a better way?

> > > Many thanks
> > > --
> > > FFMG
> > > Please remove _NOSPAM in email address for reply.



Tue, 10 Feb 2004 23:05:16 GMT  
 CString, char && delete
Hi,
Here is the code again without the mistakes

void CMyApp::A_Funtion()
{
ZeroMemory( m_struct, sizeof( CMyStruct));
CString A = "Hello A";
m_struct->A = new char[ A.GetLength() +1 ];
strcpy( m_struct->A, A );
m_struct->A[ A.GetLength() ] = '\0';

CString B = "Hello B";
m_struct->B = new char[ B.GetLength() +1 ];
strcpy( m_struct->B, B );
m_struct->B[ B.GetLength() ] = '\0';

Quote:
}

void CMyApp::MainLoop()
{
    m_struct = new CMyStruct;
    A_Function();
    //....
    // Do some more work

    delete [] m_struct->A;
    delete [] m_struct->B;
    delete m_struct;

Quote:
}

The difference between this code and the actual code is the structure has 7
members, (either int or LPSTR).

--
FFMG
Please remove _NOSPAM in email address for reply.

Quote:
> > secondly I could put it in the destructor but the fact remains that I
> cannot
> > delete it.
> > It always crashes, (delete [] m_struct->B is crashing).

> > I don't know if this will help but the Assert message is

> > File:dbgheap.c
> > Line:1109

> > Expression: _pFirstBlock == pHead

> > I can use m_struct->B without any problems. Even if I put a break just
> > before to the delete I can still see that the data is valid.
> > I just can't see why it would be crashing.



Tue, 10 Feb 2004 23:14:58 GMT  
 CString, char && delete
Doug, et al.,
  Geez... just when I think I have things figured out... ;)  Thanks (and to
the others responding similar info) for updating/correcting me... (next time
I'll be sure to try my suggestions BEFORE I open my big mouth and insert my
size 22 foot)... <sheepish grin>
wardo



Quote:

> >FFMG,
> >   you're trying to copy a string from a CString object pointer...
> >> strcpy( m_struct.A, A );
> >   instead of
> >> strcpy( m_struct.A, (LPCTSTR)A );

> Due to CString::operatr LPCTSTR, the two are equivalent. You do need
> to cast when passing CStrings to printf and other variadic functions,
> but strcpy's second parameter is const char*, so the cast is
> superfluous for strcpy.

> --
> Doug Harrison [VC++ MVP]
> Eluent Software, LLC
> http://www.eluent.com
> Tools for Visual C++ and Windows



Tue, 10 Feb 2004 23:48:17 GMT  
 CString, char && delete
Can you publish the class definition of m_struct.


Quote:
> Hi,
> Here is the code again without the mistakes

> void CMyApp::A_Funtion()
> {
> ZeroMemory( m_struct, sizeof( CMyStruct));
> CString A = "Hello A";
> m_struct->A = new char[ A.GetLength() +1 ];
> strcpy( m_struct->A, A );
> m_struct->A[ A.GetLength() ] = '\0';

> CString B = "Hello B";
> m_struct->B = new char[ B.GetLength() +1 ];
> strcpy( m_struct->B, B );
> m_struct->B[ B.GetLength() ] = '\0';
> }

> void CMyApp::MainLoop()
> {
>     m_struct = new CMyStruct;
>     A_Function();
>     //....
>     // Do some more work

>     delete [] m_struct->A;
>     delete [] m_struct->B;
>     delete m_struct;
> }

> The difference between this code and the actual code is the structure has
7
> members, (either int or LPSTR).

> --
> FFMG
> Please remove _NOSPAM in email address for reply.

> > > secondly I could put it in the destructor but the fact remains that I
> > cannot
> > > delete it.
> > > It always crashes, (delete [] m_struct->B is crashing).

> > > I don't know if this will help but the Assert message is

> > > File:dbgheap.c
> > > Line:1109

> > > Expression: _pFirstBlock == pHead

> > > I can use m_struct->B without any problems. Even if I put a break just
> > > before to the delete I can still see that the data is valid.
> > > I just can't see why it would be crashing.



Tue, 10 Feb 2004 23:56:44 GMT  
 CString, char && delete
typedef struct
  {
    unsigned long ulReserved;                   // Reserved for future use
    LPSTR lpszName;
    LPSTR lpszSurname;
    LPSTR lpszEmployeeType;
    LPSTR lpszDateStarted;
    LPSTR lpszConversationID;
    unsigned long nYearCount;
  } UNALIGNED intStruct, UNALIGNED FAR * lpintStruct;

intStruct stands for internal structure.
lpszDateStarted is the one that always crashes.
we always use pointers and not objects.
There is no constructors/ destructors (yet).

--
FFMG
Please remove _NOSPAM in email address for reply.

Quote:
> Can you publish the class definition of m_struct.



> > Hi,
> > Here is the code again without the mistakes

> > void CMyApp::A_Funtion()
> > {
> > ZeroMemory( m_struct, sizeof( CMyStruct));
> > CString A = "Hello A";
> > m_struct->A = new char[ A.GetLength() +1 ];
> > strcpy( m_struct->A, A );
> > m_struct->A[ A.GetLength() ] = '\0';

> > CString B = "Hello B";
> > m_struct->B = new char[ B.GetLength() +1 ];
> > strcpy( m_struct->B, B );
> > m_struct->B[ B.GetLength() ] = '\0';
> > }

> > void CMyApp::MainLoop()
> > {
> >     m_struct = new CMyStruct;
> >     A_Function();
> >     //....
> >     // Do some more work

> >     delete [] m_struct->A;
> >     delete [] m_struct->B;
> >     delete m_struct;
> > }

> > The difference between this code and the actual code is the structure
has
> 7
> > members, (either int or LPSTR).

> > --
> > FFMG
> > Please remove _NOSPAM in email address for reply.

> > > > secondly I could put it in the destructor but the fact remains that
I
> > > cannot
> > > > delete it.
> > > > It always crashes, (delete [] m_struct->B is crashing).

> > > > I don't know if this will help but the Assert message is

> > > > File:dbgheap.c
> > > > Line:1109

> > > > Expression: _pFirstBlock == pHead

> > > > I can use m_struct->B without any problems. Even if I put a break
just
> > > > before to the delete I can still see that the data is valid.
> > > > I just can't see why it would be crashing.



Wed, 11 Feb 2004 00:03:46 GMT  
 CString, char && delete


Quote:
> Hi,
> I have a program

[ code to copy a couple of strings into a struct elided ]

Quote:
> void CMyApp::MainLoop()
> {
>     m_struct = new CMyStruct;
>     A_Function();

Nothing you've shown us looks like it should be causing a problem.  
The declaration of CMyStruct _might_ have some relevance, though it's
a bit hard to say.  We're probably not going to be able to provide a
lot of real help until/unless you do your part: cut your code down to
the minimal that still displays the problem, and then post it so we
can actually see the problem.

Quote:
>     //....
>     // Do some more work

Based on what you've posted, the "more work" may easily be where
you're causing the problem, but it's impossible to really guess
without knowing that that is.  If you corrupt one of your pointers
(for one example) you can end up using data that doesn't really
belong to you.

Quote:
> How should it be done? Is there a better way?

Probably.  It's a bit hard to say since you haven't showed up what
you're doing with things, but offhand the code looks like almost the
antithesis of object oriented -- you have a CMyStruct, but it's code
outside of CMyStruct that's manipulating its contents.  There's a
pretty fair chance that most (perhaps all) of the code you've shown
us should really be member functions of CMyStruct (perhaps suitably
renamed to reflect the fact that it's not just dumb data.  Based on
what you've shown so far, I'd envision something like this:

class MyData {
        CString A;
        CString B;
public:
        MyData(CString init_A, CString init_b)
                : A(init_A), B(init_B)
        {}

        void DoWork() {
                // Do some more work
                // converting CStrings to char *'s ONLY as needed.
        }

Quote:
};

void CMyApp::MainLoop() {
        MyData data("Hello_A", "Hello_B");

        data.DoWork();

Quote:
}

--
    Later,
    Jerry.

The Universe is a figment of its own imagination.



Thu, 12 Feb 2004 00:59:36 GMT  
 CString, char && delete
Hi,
Thanks for the reply.

I did post all the code. In my program I removed all the code and all I have
is
1)new structure
2)Call a function with new items in it
3)delete the structure.

The structure is the way I showed it in the previous posts, here it is in
case you did not get it

typedef struct
  {
    unsigned long ulReserved;                   // Reserved for future use
    LPSTR lpszName;
    LPSTR lpszSurname;
    LPSTR lpszEmployeeType;
    LPSTR lpszDateStarted;
    LPSTR lpszConversationID;
    unsigned long nYearCount;
  } UNALIGNED intStruct, UNALIGNED FAR * lpintStruct;

After moving things around in the function I have noticed that it is the
last item 'new' that would crash on delete.
For example if I did

void my_fuction()
{
ZeroMemory( m_struct, sizeof( CMyStruct));
CString A = "Hello A";
m_struct->A = new char[ A.GetLength() +1 ];
strcpy( m_struct->A, A );

CString B = "Hello B";
m_struct->B = new char[ B.GetLength() +1 ];
strcpy( m_struct->B, B );

Quote:
}

delete [] m_struct->B would crash
and vise versa if I placed m_struct->B first then m_struct->A would crash??

Last but not least the function that I am calling is in a separate DLL. But
it is a simple function call to a DLL,
Again no other work is done as I have disabled all the code in between.

Thanks for the help.
--
FFMG
Please remove _NOSPAM in email address for reply.

Quote:


> > Hi,
> > I have a program

> [ code to copy a couple of strings into a struct elided ]

> > void CMyApp::MainLoop()
> > {
> >     m_struct = new CMyStruct;
> >     A_Function();

> Nothing you've shown us looks like it should be causing a problem.
> The declaration of CMyStruct _might_ have some relevance, though it's
> a bit hard to say.  We're probably not going to be able to provide a
> lot of real help until/unless you do your part: cut your code down to
> the minimal that still displays the problem, and then post it so we
> can actually see the problem.

> >     //....
> >     // Do some more work

> Based on what you've posted, the "more work" may easily be where
> you're causing the problem, but it's impossible to really guess
> without knowing that that is.  If you corrupt one of your pointers
> (for one example) you can end up using data that doesn't really
> belong to you.

> > How should it be done? Is there a better way?

> Probably.  It's a bit hard to say since you haven't showed up what
> you're doing with things, but offhand the code looks like almost the
> antithesis of object oriented -- you have a CMyStruct, but it's code
> outside of CMyStruct that's manipulating its contents.  There's a
> pretty fair chance that most (perhaps all) of the code you've shown
> us should really be member functions of CMyStruct (perhaps suitably
> renamed to reflect the fact that it's not just dumb data.  Based on
> what you've shown so far, I'd envision something like this:

> class MyData {
> CString A;
> CString B;
> public:
>   MyData(CString init_A, CString init_b)
> : A(init_A), B(init_B)
> {}

> void DoWork() {
> // Do some more work
> // converting CStrings to char *'s ONLY as needed.
> }
> };

> void CMyApp::MainLoop() {
> MyData data("Hello_A", "Hello_B");

> data.DoWork();
> }

> --
>     Later,
>     Jerry.

> The Universe is a figment of its own imagination.



Thu, 12 Feb 2004 04:15:04 GMT  
 CString, char && delete

Quote:

>delete [] m_struct->B would crash
>and vise versa if I placed m_struct->B first then m_struct->A would crash??

>Last but not least the function that I am calling is in a separate DLL. But
>it is a simple function call to a DLL,

It sounds like you may be allocating memory in one dynamically linked
module and freeing it in another, which is a problem if the modules
aren't using the same CRT, for in that case, they have different
heaps, C-level file descriptors, and other CRT state. If you want your
program to act like a statically linked program with respect to CRT
state, you'll need to dynamically link every module to the same CRT
DLL. See for example the compiler options -MD and -MDd. Otherwise,
you're going to have to be very careful crossing module boundaries,
which can be a real headache.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Thu, 12 Feb 2004 05:11:28 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. CString& strMyString or CString &strMyString

2. Help: inline char const* const& max(char const* const &a, char const* const &b)

3. CCmdTarget && delete && CList

4. ***&&&>>> HELP --- CXL --- HELP <<<&&&****

5. CString to Char and Char to CString

6. char[100] to CString and CString to char[100] again

7. mktemp(char * tempname) & mkstemp(char * tempname)

8. The differences between char * & char []

9. signed char & unsigned char

10. char *,char[],&,Question

11. Linker error 2001 @&(#&@#&$@

12. Linker error 2001 @&(#&@#&$@

 

 
Powered by phpBB® Forum Software