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

Discussion in 'C++' started by Angus, Feb 13, 2007.

  1. Angus

    Angus Guest

    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?
    Angus, Feb 13, 2007
    #1
    1. Advertising

  2. Angus

    Rolf Magnus Guest

    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.
    Rolf Magnus, Feb 13, 2007
    #2
    1. Advertising

  3. 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.
    Jacek Dziedzic, Feb 13, 2007
    #3
  4. Angus

    Angus Guest

    On 13 Feb, 11:03, Jacek Dziedzic
    <> 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.
    Angus, Feb 13, 2007
    #4
  5. Angus

    Angus Guest

    Angus wrote:

    > On 13 Feb, 11:03, Jacek Dziedzic
    > <> 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.
    Angus, Feb 13, 2007
    #5
  6. Angus

    peter koch Guest

    On 13 Feb., 10:53, "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.

    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
    peter koch, Feb 13, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HeroOfSpielburg
    Replies:
    1
    Views:
    390
    Alf P. Steinbach
    Aug 6, 2003
  2. Robert Brewer
    Replies:
    1
    Views:
    487
    bsmith
    Nov 7, 2004
  3. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,520
    Carl Banks
    Feb 23, 2005
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    914
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,661
    Richard Bos
    May 17, 2010
Loading...

Share This Page