dyn.alloc error

Discussion in 'C++' started by JDHawk, Nov 14, 2007.

  1. JDHawk

    JDHawk Guest

    Hi,

    when I compile my c++ program, MS Visual C++ (2003) shows this error:
    Damage: after Normal Block (#139) at 0xXXXXXXXX

    In Debug Mode I found out that it is the deallocation with "delete" in
    the destructor.
    When hiding the delete, there is no error, but I want to set my data
    free in the destructor before the object dies. It has something to do
    with the array that is allocated, cause a deletion of the ponter
    "p_crc" works perfectly. Here the code: for explanation:

    //send_frame is a struct with an pointer to uint8 as one element
    //typedef signed char uint8;
    //uint8* data;

    //Ctor
    Segmenter::Segmenter() : length(0), seg_number(0), seg_count(0),
    retries(0)
    {
    //cout << "Konstruktor Segmenter" << std::endl;
    send_frame.data = new uint8[DATA_LEN];//
    <-------------------------------allocation works
    rcvd_frame.data = new uint8[DATA_LEN];//
    <--------------------------------allocation works

    //send_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    //rcvd_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    p_crc = new Crc16_MC;//
    <----------------------------------------------------allocation works
    assert(p_crc);
    }


    //Dtor
    Segmenter::~Segmenter()
    {
    cout << "Destruktor Segmenter" << std::endl;
    //free(send_frame.data);
    //free(rcvd_frame.data);
    delete [] send_frame.data; //<-------------------------------ERROR
    delete [] rcvd_frame.data; //<-------------------------------ERROR
    delete p_crc; //<-------------------------------------------------
    works fine
    }
    JDHawk, Nov 14, 2007
    #1
    1. Advertising

  2. JDHawk

    Stefan Naewe Guest

    On 11/14/2007 2:24 PM, JDHawk wrote:
    > Hi,
    >
    > when I compile my c++ program, MS Visual C++ (2003) shows this error:
    > Damage: after Normal Block (#139) at 0xXXXXXXXX
    >
    > In Debug Mode I found out that it is the deallocation with "delete" in
    > the destructor.
    > When hiding the delete, there is no error, but I want to set my data
    > free in the destructor before the object dies. It has something to do
    > with the array that is allocated, cause a deletion of the ponter
    > "p_crc" works perfectly. Here the code: for explanation:
    >
    > //send_frame is a struct with an pointer to uint8 as one element
    > //typedef signed char uint8;
    > //uint8* data;
    >
    > //Ctor
    > Segmenter::Segmenter() : length(0), seg_number(0), seg_count(0),
    > retries(0)
    > {
    > //cout << "Konstruktor Segmenter" << std::endl;
    > send_frame.data = new uint8[DATA_LEN];//
    > <-------------------------------allocation works
    > rcvd_frame.data = new uint8[DATA_LEN];//
    > <--------------------------------allocation works
    >
    > //send_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    > //rcvd_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    > p_crc = new Crc16_MC;//
    > <----------------------------------------------------allocation works
    > assert(p_crc);
    > }
    >
    >
    > //Dtor
    > Segmenter::~Segmenter()
    > {
    > cout << "Destruktor Segmenter" << std::endl;
    > //free(send_frame.data);
    > //free(rcvd_frame.data);
    > delete [] send_frame.data; //<-------------------------------ERROR
    > delete [] rcvd_frame.data; //<-------------------------------ERROR
    > delete p_crc; //<-------------------------------------------------
    > works fine
    > }
    >


    Did you implement the Copy C'tor _correctly_ ?
    (Why not use a std::vector ?) ?

    S.
    --
    Stefan Naewe stefan dot naewe at atlas-elektronik dot com
    Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
    Plain text mails only, please http://www.expita.com/nomime.html
    Stefan Naewe, Nov 14, 2007
    #2
    1. Advertising

  3. On Nov 14, 1:24 pm, JDHawk <> wrote:
    > Hi,
    >
    > when I compile my c++ program, MS Visual C++ (2003) shows this error:
    > Damage: after Normal Block (#139) at 0xXXXXXXXX
    >
    > In Debug Mode I found out that it is the deallocation with "delete" in
    > the destructor.
    > When hiding the delete, there is no error, but I want to set my data
    > free in the destructor before the object dies. It has something to do
    > with the array that is allocated, cause a deletion of the ponter
    > "p_crc" works perfectly. Here the code: for explanation:
    >
    > //send_frame is a struct with an pointer to uint8 as one element
    > //typedef signed char uint8;
    > //uint8* data;
    >
    > //Ctor
    > Segmenter::Segmenter() : length(0), seg_number(0), seg_count(0),
    > retries(0)
    > {
    > //cout << "Konstruktor Segmenter" << std::endl;
    > send_frame.data = new uint8[DATA_LEN];//
    > <-------------------------------allocation works
    > rcvd_frame.data = new uint8[DATA_LEN];//
    > <--------------------------------allocation works
    >
    > //send_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    > //rcvd_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    > p_crc = new Crc16_MC;//
    > <----------------------------------------------------allocation works
    > assert(p_crc);
    >
    > }
    >
    > //Dtor
    > Segmenter::~Segmenter()
    > {
    > cout << "Destruktor Segmenter" << std::endl;
    > //free(send_frame.data);
    > //free(rcvd_frame.data);
    > delete [] send_frame.data; //<-------------------------------ERROR
    > delete [] rcvd_frame.data; //<-------------------------------ERROR
    > delete p_crc; //<-------------------------------------------------
    > works fine
    >
    > }

    What happens in the meantime? Usually memory damage like that
    indicates that something's already gone wrong. Usually that's a double
    delete on the pointer or a memory overflow.

    if I write:
    int main(int argc, char** argv)
    {
    Segmenter s;
    }

    does that produce the error?
    Jonathan Lane, Nov 14, 2007
    #3
  4. On Nov 14, 1:24 pm, JDHawk <> wrote:
    > Hi,
    >
    > when I compile my c++ program, MS Visual C++ (2003) shows this error:
    > Damage: after Normal Block (#139) at 0xXXXXXXXX
    >
    > In Debug Mode I found out that it is the deallocation with "delete" in
    > the destructor.
    > When hiding the delete, there is no error, but I want to set my data
    > free in the destructor before the object dies. It has something to do
    > with the array that is allocated, cause a deletion of the ponter
    > "p_crc" works perfectly. Here the code: for explanation:
    >
    > //send_frame is a struct with an pointer to uint8 as one element
    > //typedef signed char uint8;
    > //uint8* data;
    >
    > //Ctor
    > Segmenter::Segmenter() : length(0), seg_number(0), seg_count(0),
    > retries(0)
    > {
    > //cout << "Konstruktor Segmenter" << std::endl;
    > send_frame.data = new uint8[DATA_LEN];//
    > <-------------------------------allocation works
    > rcvd_frame.data = new uint8[DATA_LEN];//
    > <--------------------------------allocation works
    >
    > //send_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    > //rcvd_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    > p_crc = new Crc16_MC;//
    > <----------------------------------------------------allocation works
    > assert(p_crc);
    >
    > }
    >
    > //Dtor
    > Segmenter::~Segmenter()
    > {
    > cout << "Destruktor Segmenter" << std::endl;
    > //free(send_frame.data);
    > //free(rcvd_frame.data);
    > delete [] send_frame.data; //<-------------------------------ERROR
    > delete [] rcvd_frame.data; //<-------------------------------ERROR
    > delete p_crc; //<-------------------------------------------------
    > works fine
    >
    >
    >


    It's a common mistake to assume that because an error occurs at
    a particular deallocation, the fault must lie there or in the
    corresponding allocation. In fact what's almost certainly happened
    is some other part of your program (which you haven't posted)
    is corrupting the heap, whereupon basically all bets are off.
    I'd start by checking that you're not overrunning the bounds of
    either send_frame.data or rcvd_frame.data (the perils of using
    raw arrays, std::vector would be better here for precisely this
    reason), and then broaden the search.
    tragomaskhalos, Nov 14, 2007
    #4
  5. JDHawk

    JDHawk Guest

    On 14 Nov., 14:42, Stefan Naewe <> wrote:
    > On 11/14/2007 2:24 PM, JDHawk wrote:
    >
    >
    >
    > > Hi,

    >
    > > when I compile my c++ program, MS Visual C++ (2003) shows this error:
    > > Damage: after Normal Block (#139) at 0xXXXXXXXX

    >
    > > In Debug Mode I found out that it is the deallocation with "delete" in
    > > the destructor.
    > > When hiding the delete, there is no error, but I want to set my data
    > > free in the destructor before the object dies. It has something to do
    > > with the array that is allocated, cause a deletion of the ponter
    > > "p_crc" works perfectly. Here the code: for explanation:

    >
    > > //send_frame is a struct with an pointer to uint8 as one element
    > > //typedef signed char uint8;
    > > //uint8* data;

    >
    > > //Ctor
    > > Segmenter::Segmenter() : length(0), seg_number(0), seg_count(0),
    > > retries(0)
    > > {
    > > //cout << "Konstruktor Segmenter" << std::endl;
    > > send_frame.data = new uint8[DATA_LEN];//
    > > <-------------------------------allocation works
    > > rcvd_frame.data = new uint8[DATA_LEN];//
    > > <--------------------------------allocation works

    >
    > > //send_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    > > //rcvd_frame.data = (uint8*)malloc(sizeof(uint8)*DATA_LEN);
    > > p_crc = new Crc16_MC;//
    > > <----------------------------------------------------allocation works
    > > assert(p_crc);
    > > }

    >
    > > //Dtor
    > > Segmenter::~Segmenter()
    > > {
    > > cout << "Destruktor Segmenter" << std::endl;
    > > //free(send_frame.data);
    > > //free(rcvd_frame.data);
    > > delete [] send_frame.data; //<-------------------------------ERROR
    > > delete [] rcvd_frame.data; //<-------------------------------ERROR
    > > delete p_crc; //<-------------------------------------------------
    > > works fine
    > > }

    >
    > Did you implement the Copy C'tor _correctly_ ?
    > (Why not use a std::vector ?) ?
    >
    > S.
    > --
    > Stefan Naewe stefan dot naewe at atlas-elektronik dot com
    > Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
    > Plain text mails only, please http://www.expita.com/nomime.html


    I got the error:
    I added one '\0' at the end of data and so exceeded the dyn. range.

    Thank You very much
    JDHawk, Nov 14, 2007
    #5
    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. Bob
    Replies:
    1
    Views:
    606
    .NET Follower
    Feb 13, 2004
  2. cerr

    Queue question wih dyn mem alloc

    cerr, Dec 28, 2009, in forum: C Programming
    Replies:
    14
    Views:
    550
    Barry Schwarz
    Dec 31, 2009
  3. cerr
    Replies:
    6
    Views:
    258
    ImpalerCore
    Mar 17, 2011
  4. Alexander Ryck

    2 dyn. select-menus in 1 Form

    Alexander Ryck, Mar 6, 2004, in forum: Javascript
    Replies:
    3
    Views:
    114
    Alexander Ryck
    Mar 7, 2004
  5. Replies:
    0
    Views:
    81
Loading...

Share This Page