473,785 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hello

I am doing this in a worker thread:

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

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_cas t<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?

Feb 13 '07 #1
5 2134
Angus wrote:
Hello

I am doing this in a worker thread:

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

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.

Feb 13 '07 #2
Angus wrote:
Hello

I am doing this in a worker thread:

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

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_cas t<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.
Feb 13 '07 #3
On 13 Feb, 11:03, Jacek Dziedzic
<jacek.dziedzic .n.o.s.p....@gm ail.comwrote:
Angus wrote:
Hello
I am doing this in a worker thread:
char* szPartial = new char[strlen(szBuffer )+1];
if (szPartial)
{
lstrcpy(szParti al, szBuffer);
PostMessage(m_t hishWnd, WM_USER+1, 0, (LPARAM)szParti al);
}
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_cas t<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.


Feb 13 '07 #4

Angus wrote:
On 13 Feb, 11:03, Jacek Dziedzic
<jacek.dziedzic .n.o.s.p....@gm ail.comwrote:
Angus wrote:
Hello
I am doing this in a worker thread:
char* szPartial = new char[strlen(szBuffer )+1];
if (szPartial)
{
lstrcpy(szParti al, szBuffer);
PostMessage(m_t hishWnd, WM_USER+1, 0, (LPARAM)szParti al);
}
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_cas t<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.

Feb 13 '07 #5
On 13 Feb., 10:53, "Angus" <anguscom...@gm ail.comwrote:
Hello

I am doing this in a worker thread:

char* szPartial = new char[strlen(szBuffer )+1];
if (szPartial)
{
lstrcpy(szParti al, szBuffer);
PostMessage(m_t hishWnd, WM_USER+1, 0, (LPARAM)szParti al);

}

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.publi c.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_cas t<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

Feb 13 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2338
by: Martin Magnusson | last post by:
I'm getting segmentation faults when trying to fix a memory leak in my program. The problem is related to lists of pointers which get passed around between objects. Here is a description of how the code works: I have a stack consisting of nodes and arguments, much like a function execution stack. In this example there are only three nodes on the stack. For each run through the loop, a copy of the current sensation is written to the...
5
2514
by: George | last post by:
I cannot understand how to fix my code so that it will work. It needs to draw a series of lines in different colors and save it to a ppm file. Could someone tell me what I have done wrong. Thanks a lot. #include <stdio.h> #include <stdlib.h> #include <assert.h> struct Pixel {
25
4920
by: John Hanley | last post by:
I have a program where both my main.c and program.c files use the program.h file. So I #include "program.h" in both the .c files. The program.h file has #ifndef PROGRAM_H #define PROGRAM_H .... #endif Yet when I build my program, the DJGPP compiler tells me there are multiple definitions of each of my functions.
9
10904
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent the wheel, right? Everything so far is working well with the Active Directory. The problem I am having is with adding File Permissions to a directory. I am currently using some code courtesy of "Willy Denoyette "
9
2333
by: Bern McCarty | last post by:
I am porting stuff from MEC++ syntax to the new C++/CLI syntax. Something that we did in the old syntax that proved to be very valuable was to make sure that the finalizer would purposefully generate an assertion failure for unoptimized, debug builds. We did this to find and fix cases where we were relying upon finalization rather than pro-active Dispose() calls. For classes that introduced IDisposable() into the class hierarchy...
10
2967
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
1
2099
by: sharekhan | last post by:
Hello, I have written a simple relay server. using pthreads. To handle multiple clients. Design wise its like this.. (my humble apologies if the post is too long) The thread design was borrowed from this article (http://www.codeproject.com/useritems/LikeJavaThreads.asp)
71
19136
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a problem? I cannot see why using malloc instead of new does not give the same result.
9
2059
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded the + operator by means of a friend function. Everything worked fine until I decided to use a variable array size (by using new/delete), now I get an error when a temporary object is deleted (e.g. after addition), the error occurs at the delete...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.