Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using new in one thread and delete in another - getting assert

Reply
Thread Tools

Using new in one thread and delete in another - getting assert

 
 
Angus
Guest
Posts: n/a
 
      02-13-2007
Hello

I am doing this in a worker thread:

char* szPartial = new char[strlen(szBuffer)+1];
if (szPartial)
{
lstrcpy(szPartial, szBuffer);
PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial);
}

PostMessage is platofrm dependent but it is not particulalry relevant
to this problem. It is just a mechanism to pass a message to another
thread in Windows.

In the thread passed to, I do this:

// [lParam is the variable name of the dxPartial passed].
char* sz = reinterpret_cast<char*>(lParam);
std::string str = sz;
delete [] sz;

Get an assert on the delete [] sz; - debug msg is:
DAMAGE: after Normal block (#56) at 0x008C4570

But why? What am I doing wrong?

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-13-2007
Angus wrote:

> Hello
>
> I am doing this in a worker thread:
>
> char* szPartial = new char[strlen(szBuffer)+1];
> if (szPartial)
> {
> lstrcpy(szPartial, szBuffer);
> PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial);
> }
>
> PostMessage is platofrm dependent but it is not particulalry relevant
> to this problem. It is just a mechanism to pass a message to another
> thread in Windows.


Well, threads are relevant, and they are platform dependant too. In standard
C++, they don't even exist.

 
Reply With Quote
 
 
 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      02-13-2007
Angus wrote:
> Hello
>
> I am doing this in a worker thread:
>
> char* szPartial = new char[strlen(szBuffer)+1];
> if (szPartial)
> {
> lstrcpy(szPartial, szBuffer);
> PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial);
> }
>
> PostMessage is platofrm dependent but it is not particulalry relevant
> to this problem. It is just a mechanism to pass a message to another
> thread in Windows.
>
> In the thread passed to, I do this:
>
> // [lParam is the variable name of the dxPartial passed].
> char* sz = reinterpret_cast<char*>(lParam);
> std::string str = sz;
> delete [] sz;
>
> Get an assert on the delete [] sz; - debug msg is:
> DAMAGE: after Normal block (#56) at 0x008C4570
>
> But why? What am I doing wrong?


Are you sure a pointer would fit into an LPARAM?
Perhaps sizeof(char*) > sizeof(LPARAM)?

Other than that, I would try printing the adress just
before delete[] and compare it with the address obtained
by new, to make sure they are in fact the same.

HTH,
- J.
 
Reply With Quote
 
Angus
Guest
Posts: n/a
 
      02-13-2007
On 13 Feb, 11:03, Jacek Dziedzic
<(E-Mail Removed)> wrote:
> Angus wrote:
> > Hello

>
> > I am doing this in a worker thread:

>
> > char* szPartial = new char[strlen(szBuffer)+1];
> > if (szPartial)
> > {
> > lstrcpy(szPartial, szBuffer);
> > PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial);
> > }

>
> > PostMessage is platofrm dependent but it is not particulalry relevant
> > to this problem. It is just a mechanism to pass a message to another
> > thread in Windows.

>
> > In the thread passed to, I do this:

>
> > // [lParam is the variable name of the dxPartial passed].
> > char* sz = reinterpret_cast<char*>(lParam);
> > std::string str = sz;
> > delete [] sz;

>
> > Get an assert on the delete [] sz; - debug msg is:
> > DAMAGE: after Normal block (#56) at 0x008C4570

>
> > But why? What am I doing wrong?

>
> Are you sure a pointer would fit into an LPARAM?
> Perhaps sizeof(char*) > sizeof(LPARAM)?
>
> Other than that, I would try printing the adress just
> before delete[] and compare it with the address obtained
> by new, to make sure they are in fact the same.
>
> HTH,
> - J.- Hide quoted text -
>
> - Show quoted text -


If I do the passing of data using a buffer - available to both threads
it works fine. so the amount of data is not the problem. But using
buffers, I get issues of buffers being overwritten if you send lots of
messages. If I remove the delete [] sz; it all works and the correct
data is passed. If I examine the text it is the same both ends.
LPARAM is an unsigned long. On my system sizeof(LPARAM) is 4,
sizeof(char*) is 4.




 
Reply With Quote
 
Angus
Guest
Posts: n/a
 
      02-13-2007

Angus wrote:

> On 13 Feb, 11:03, Jacek Dziedzic
> <(E-Mail Removed)> wrote:
> > Angus wrote:
> > > Hello

> >
> > > I am doing this in a worker thread:

> >
> > > char* szPartial = new char[strlen(szBuffer)+1];
> > > if (szPartial)
> > > {
> > > lstrcpy(szPartial, szBuffer);
> > > PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial);
> > > }

> >
> > > PostMessage is platofrm dependent but it is not particulalry relevant
> > > to this problem. It is just a mechanism to pass a message to another
> > > thread in Windows.

> >
> > > In the thread passed to, I do this:

> >
> > > // [lParam is the variable name of the dxPartial passed].
> > > char* sz = reinterpret_cast<char*>(lParam);
> > > std::string str = sz;
> > > delete [] sz;

> >
> > > Get an assert on the delete [] sz; - debug msg is:
> > > DAMAGE: after Normal block (#56) at 0x008C4570

> >
> > > But why? What am I doing wrong?

> >
> > Are you sure a pointer would fit into an LPARAM?
> > Perhaps sizeof(char*) > sizeof(LPARAM)?
> >
> > Other than that, I would try printing the adress just
> > before delete[] and compare it with the address obtained
> > by new, to make sure they are in fact the same.
> >
> > HTH,
> > - J.- Hide quoted text -
> >
> > - Show quoted text -

>
> If I do the passing of data using a buffer - available to both threads
> it works fine. so the amount of data is not the problem. But using
> buffers, I get issues of buffers being overwritten if you send lots of
> messages. If I remove the delete [] sz; it all works and the correct
> data is passed. If I examine the text it is the same both ends.
> LPARAM is an unsigned long. On my system sizeof(LPARAM) is 4,
> sizeof(char*) is 4.


Of course it was something stupid I had not noticed.

In fact on one of the PostMessage's I had this:

char* szPartial = new char[strlen(szBuffer)];

And of course the szBuffer had a \0 on the end. so I assume because
the null was past the end of the buffer it caused the problem. So I
changed to char* szPartial = new char[strlen(szBuffer)+1]; and it is
all working fine now.

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      02-13-2007
On 13 Feb., 10:53, "Angus" <(E-Mail Removed)> wrote:
> Hello
>
> I am doing this in a worker thread:
>
> char* szPartial = new char[strlen(szBuffer)+1];
> if (szPartial)
> {
> lstrcpy(szPartial, szBuffer);
> PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial);
>
> }
>
> PostMessage is platofrm dependent but it is not particulalry relevant
> to this problem. It is just a mechanism to pass a message to another
> thread in Windows.

I agree with others that this post is borderline in topicality. One
problem with your code could be that you are using the wrong libraries
and that would certainly be off-topic. I recommend you go to
microsoft.public.vc.language (if you use the Microsoft compiler) for
further investigation.
>
> In the thread passed to, I do this:
>
> // [lParam is the variable name of the dxPartial passed].
> char* sz = reinterpret_cast<char*>(lParam);
> std::string str = sz;
> delete [] sz;
>
> Get an assert on the delete [] sz; - debug msg is:
> DAMAGE: after Normal block (#56) at 0x008C4570


I believe the message is clear: somehow you managed to invalidate a
block of memory. This could be a result of insufficient protection
(wrong library) or a result of some code not shown here.

/Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To assert or not to assert... ImpalerCore C Programming 79 05-17-2010 12:47 PM
assert 0, "foo" vs. assert(0, "foo") Thomas Guettler Python 3 02-23-2005 07:53 PM
assert(x) and '#define ASSERT(x) assert(x)' Alex Vinokur C Programming 5 11-25-2004 08:48 PM
RE: remove assert statement (Was: Re: PEP new assert idiom) Robert Brewer Python 1 11-07-2004 06:53 PM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM



Advertisments