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

A

Angus

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?
 
R

Rolf Magnus

Angus said:
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.
 
J

Jacek Dziedzic

Angus said:
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.
 
A

Angus

Angus said:
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.
 
A

Angus

Angus said:
Angus said:
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.
 
P

peter koch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top