Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6341 > unrolled thread

HEAP error when trying to use free() help needed

Started bynone <none9823@hotmail.com>
First post2011-06-07 03:16 -0700
Last post2011-06-20 18:25 +0000
Articles 17 — 9 participants

Back to article view | Back to comp.lang.c++


Contents

  HEAP error when trying to use free() help needed none <none9823@hotmail.com> - 2011-06-07 03:16 -0700
    Re: HEAP error when trying to use free() help needed "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2011-06-07 13:11 +0200
    Re: HEAP error when trying to use free() help needed "Balog Pal" <pasa@lib.hu> - 2011-06-07 15:18 +0200
    Re: HEAP error when trying to use free() help needed "io_x" <a@b.c.invalid> - 2011-06-07 19:50 +0200
      Re: HEAP error when trying to use free() help needed "io_x" <a@b.c.invalid> - 2011-06-09 08:48 +0200
    Re: HEAP error when trying to use free() help needed "io_x" <a@b.c.invalid> - 2011-06-08 11:11 +0200
      Re: HEAP error when trying to use free() help needed Juha Nieminen <nospam@thanks.invalid> - 2011-06-08 10:17 +0000
    Re: HEAP error when trying to use free() help needed Mustansir <mustansir.azhar@gmail.com> - 2011-06-08 06:23 -0700
    Re: HEAP error when trying to use free() help needed Krice <paulkp@mbnet.fi> - 2011-06-08 11:42 -0700
      Re: HEAP error when trying to use free() help needed Pete Becker <pete@versatilecoding.com> - 2011-06-08 08:54 -1000
    Re: HEAP error when trying to use free() help needed none <none9823@hotmail.com> - 2011-06-19 22:06 -0700
      Re: HEAP error when trying to use free() help needed Ian Collins <ian-news@hotmail.com> - 2011-06-20 17:17 +1200
        Re: HEAP error when trying to use free() help needed none <none9823@hotmail.com> - 2011-06-19 22:53 -0700
          Re: HEAP error when trying to use free() help needed Juha Nieminen <nospam@thanks.invalid> - 2011-06-20 06:06 +0000
            Re: HEAP error when trying to use free() help needed none <none9823@hotmail.com> - 2011-06-20 00:35 -0700
              Re: HEAP error when trying to use free() help needed Ian Collins <ian-news@hotmail.com> - 2011-06-20 20:57 +1200
              Re: HEAP error when trying to use free() help needed Juha Nieminen <nospam@thanks.invalid> - 2011-06-20 18:25 +0000

#6341 — HEAP error when trying to use free() help needed

Fromnone <none9823@hotmail.com>
Date2011-06-07 03:16 -0700
SubjectHEAP error when trying to use free() help needed
Message-ID<75366d67-4c83-46cf-a75b-6a953578b6f8@v12g2000vby.googlegroups.com>
I'm trying to figure out why this testing code do not work and produce
an error in VC++ when returning from the foo method.

here's the code:

    class test {
    public:
        char* pPointer;

        test (void) {
           pPointer = (char *) malloc (sizeof(char));

        }

        ~test () {
            free (pPointer); // HEAP error! when returning from foo
        }

        test test::foo (void) {
            test myfoo;
            return myfoo;
        }
    };

    ...

    test atest;
    atest = atest.foo(); // HEAP error!

    The pPointer seems to be already freed by foo, why is this? How
can I make this code work?

    Thanks in advance

[toc] | [next] | [standalone]


#6343

From"Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl>
Date2011-06-07 13:11 +0200
Message-ID<isl10m$31q$1@news.albasani.net>
In reply to#6341
"none"  wrote in message 
news:75366d67-4c83-46cf-a75b-6a953578b6f8@v12g2000vby.googlegroups.com...

>I'm trying to figure out why this testing code do not work and produce
>an error in VC++ when returning from the foo method.
>
>here's the code:
>
>    class test {
>    public:
>        char* pPointer;
>
>        test (void) {
>           pPointer = (char *) malloc (sizeof(char));
>
>        }
>
>        ~test () {
>            free (pPointer); // HEAP error! when returning from foo
>        }
>
>        test test::foo (void) {
>            test myfoo;
>            return myfoo;
>       }
>    };
>
>    ...
>
>    test atest;
>    atest = atest.foo(); // HEAP error!
>
>    The pPointer seems to be already freed by foo, why is this? How
>can I make this code work?
>
>    Thanks in advance

Yes, since you are copying the test objects, the heap is freed twice.
Google for "Rule of three in C++". 

[toc] | [prev] | [next] | [standalone]


#6351

From"Balog Pal" <pasa@lib.hu>
Date2011-06-07 15:18 +0200
Message-ID<isl8f3$cv6$1@news.ett.com.ua>
In reply to#6341
"none" <none9823@hotmail.com> 
>    class test {
>    public:
>        char* pPointer;
> 
>        test (void) {
>           pPointer = (char *) malloc (sizeof(char));
> 
>        }
> 
>        ~test () {
>            free (pPointer); // HEAP error! when returning from foo
>        }
> 

Look up "c++ rule of 3"

[toc] | [prev] | [next] | [standalone]


#6366

From"io_x" <a@b.c.invalid>
Date2011-06-07 19:50 +0200
Message-ID<4dee6480$0$38643$4fafbaef@reader1.news.tin.it>
In reply to#6341
"none" <none9823@hotmail.com> ha scritto nel messaggio
news:75366d67-4c83-46cf-a75b-6a953578b6f8@v12g2000vby.googlegroups.com...
> I'm trying to figure out why this testing code do not work and produce
> an error in VC++ when returning from the foo method.
>
> here's the code:
>
>    class test {
>    public:
>        char* pPointer;
>
>        test (void) {
>           pPointer = (char *) malloc (sizeof(char));

here pPointer point to a char
so the name should be "cPointer" or
"pPointer = (char *) malloc (sizeof(char*));"

>        }
>
>        ~test () {
>            free (pPointer); // HEAP error! when returning from foo
>        }
>
>        test test::foo (void) {
>            test myfoo;
>            return myfoo;

could be foo function return myfoo
but when this function end '}'
the object is deleted
so if it is deleted what copy...

>        }
>    };
>
>    ...
>
>    test atest;
>    atest = atest.foo(); // HEAP error!

...here?

>    The pPointer seems to be already freed by foo, why is this? How
> can I make this code work?
>
>    Thanks in advance


[toc] | [prev] | [next] | [standalone]


#6472

From"io_x" <a@b.c.invalid>
Date2011-06-09 08:48 +0200
Message-ID<4df06c7d$0$13985$4fafbaef@reader2.news.tin.it>
In reply to#6366
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4dee6480$0$38643$4fafbaef@reader1.news.tin.it...
>
> "none" <none9823@hotmail.com> ha scritto nel messaggio
> news:75366d67-4c83-46cf-a75b-6a953578b6f8@v12g2000vby.googlegroups.com...
>> I'm trying to figure out why this testing code do not work and produce
>> an error in VC++ when returning from the foo method.
>>
>> here's the code:
>>
>>    class test {
>>    public:
>>        char* pPointer;
>>
>>        test (void) {
>>           pPointer = (char *) malloc (sizeof(char));
>
> here pPointer point to a char
> so the name should be "cPointer" or
> "pPointer = (char *) malloc (sizeof(char*));"

>>        }
>>
>>        ~test () {
>>            free (pPointer); // HEAP error! when returning from foo
>>        }
>>
>>        test test::foo (void) {
>>            test myfoo;
>>            return myfoo;
>
> could be foo function return myfoo
> but when this function end '}'
> the object is deleted
> so if it is deleted what copy...
>
>>        }
>>    };
>>
>>    ...
>>
>>    test atest;
>>    atest = atest.foo(); // HEAP error!
>
> ...here?

wrong in C++ is possible; the problem is just the double free of
myfoo.pPointer from the test distrcutor (one for myfoo the other for atest
that have the same pointer value)

>>    The pPointer seems to be already freed by foo, why is this? How
>> can I make this code work?
>>
>>    Thanks in advance
>
>
>




[toc] | [prev] | [next] | [standalone]


#6408

From"io_x" <a@b.c.invalid>
Date2011-06-08 11:11 +0200
Message-ID<4def3c6c$0$18243$4fafbaef@reader2.news.tin.it>
In reply to#6341
"none" <none9823@hotmail.com> ha scritto nel messaggio
news:75366d67-4c83-46cf-a75b-6a953578b6f8@v12g2000vby.googlegroups.com...
> I'm trying to figure out why this testing code do not work and produce
> an error in VC++ when returning from the foo method.
>
> here's the code:
>
>    class test {
>    public:
>        char* pPointer;
>
>        test (void) {
>           pPointer = (char *) malloc (sizeof(char));
>
>        }
>
>        ~test () {
>            free (pPointer); // HEAP error! when returning from foo
>        }
>
>        test test::foo (void) {
>            test myfoo;
>            return myfoo;
>        }
>    };
>
>    ...
>
>    test atest;
>    atest = atest.foo(); // HEAP error!
>
>    The pPointer seems to be already freed by foo, why is this? How
> can I make this code work?

this is my try,
what does it? assign 255 to what pointer in atest point
i don't know if it is right, we are here for know where is wrong

#include <iostream.h>
#include <stdint.h>

#define  u32  uint32_t
#define  u8   uint8_t

class test {
  public:
  u8* cPointer;

  test(){cPointer=(u8*) malloc(sizeof(u8));
         printf("%p|", cPointer);
        }
 ~test(){printf("~%p|", cPointer);
         free(cPointer);
        }
  test(u8  a)
        {cPointer=(u8*) malloc(sizeof(u8));
         printf("%p|", cPointer);
         *cPointer=a;
        }

  test& fooVoid(void)
     {static test   myfoo(255);
      return myfoo;
     }

  test&  operator=(const test&  a)
  {*cPointer=*a.cPointer;
   return *this;
  }

};

int main(void)
{test atest;
 u32      v;

 atest = atest.fooVoid();
 cout << "atest.cPointer==" << (void*)atest.cPointer << "\n";
 v=*atest.cPointer;
 cout << "atest.cPointer.value==" << v << "\n";

 return 0;
}



00862FD4|00862FE4|atest.cPointer==862fd4
atest.cPointer.value==255
~00862FD4|~00862FE4|

>    Thanks in advance



[toc] | [prev] | [next] | [standalone]


#6412

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-06-08 10:17 +0000
Message-ID<4def4c3a$0$2831$7b1e8fa0@news.nbl.fi>
In reply to#6408
io_x <a@b.c.invalid> wrote:
>  test& fooVoid(void)
>     {static test   myfoo(255);
>      return myfoo;
>     }

  Your solution to the lack of a copy constructor is to make a local 
static object which you return? That will only *mask* the problem, not 
solve it.

>  test&  operator=(const test&  a)
>  {*cPointer=*a.cPointer;
>   return *this;
>  }

  That assignment operator is horribly broken. (Hint: It will only copy 
the first character of the array.)

[toc] | [prev] | [next] | [standalone]


#6421

FromMustansir <mustansir.azhar@gmail.com>
Date2011-06-08 06:23 -0700
Message-ID<61a17603-ea2c-43b5-ae7b-999ca01ab810@z7g2000prh.googlegroups.com>
In reply to#6341
On Jun 7, 3:16 pm, none <none9...@hotmail.com> wrote:
> I'm trying to figure out why this testing code do not work and produce
> an error in VC++ when returning from the foo method.
>
> here's the code:
>
>     class test {
>     public:
>         char* pPointer;
>
>         test (void) {
>            pPointer = (char *) malloc (sizeof(char));
>
>         }
>
>         ~test () {
>             free (pPointer); // HEAP error! when returning from foo
>         }
>
>         test test::foo (void) {
>             test myfoo;
>             return myfoo;
>         }
>     };
>
>     ...
>
>     test atest;
>     atest = atest.foo(); // HEAP error!
>
>     The pPointer seems to be already freed by foo, why is this? How
> can I make this code work?
>
>     Thanks in advance

You should write a copy constructor and copy assignment operator that
performs "deep copy". Your compiler synthesized versions of these just
perform "shallow copy" by copying the pointer rather than the contents
pointed to.

[toc] | [prev] | [next] | [standalone]


#6435

FromKrice <paulkp@mbnet.fi>
Date2011-06-08 11:42 -0700
Message-ID<cd9848a6-bd90-4742-b467-ef8cb9bb6d3b@16g2000yqy.googlegroups.com>
In reply to#6341
On 7 kesä, 13:16, none <none9...@hotmail.com> wrote:
> How can I make this code work?

Don't use malloc/free in C++. Use new & delete.

[toc] | [prev] | [next] | [standalone]


#6436

FromPete Becker <pete@versatilecoding.com>
Date2011-06-08 08:54 -1000
Message-ID<2011060808542076921-pete@versatilecodingcom>
In reply to#6435
On 2011-06-08 08:42:30 -1000, Krice said:

> On 7 kesä, 13:16, none <none9...@hotmail.com> wrote:
>> How can I make this code work?
> 
> Don't use malloc/free in C++. Use new & delete.

That won't fix the underlying problem.

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

[toc] | [prev] | [next] | [standalone]


#6918

Fromnone <none9823@hotmail.com>
Date2011-06-19 22:06 -0700
Message-ID<445a602a-ca39-4806-88bf-be5820678d7f@gv8g2000vbb.googlegroups.com>
In reply to#6341
Thanks, everyone... sorry it took so long to reply...

Here's the code I came up with:

    class test {
    public:


        test (void) {
            pPointer = (char *) malloc (sizeof(char));

        }

        // Copy constructor
        test (const test& mytest) {

             pPointer = (char *) malloc (sizeof(char));
            *pPointer = *mytest.pPointer;

        }

        ~test () {
            free (pPointer);
        }

        test foo (void) {
            test myfoo;
            return myfoo;
        }

        // = operator overload
        test& operator= (const test& mytest) {

            *pPointer = *mytest.pPointer;

            return *this;

        }

        char* pPointer;
    };

Is it correct? (if we suppose that for the purpose of this test
pPointer will only point to a single char)

Thanks again

[toc] | [prev] | [next] | [standalone]


#6919

FromIan Collins <ian-news@hotmail.com>
Date2011-06-20 17:17 +1200
Message-ID<9683f5FsmeU3@mid.individual.net>
In reply to#6918
On 06/20/11 05:06 PM, none wrote:
> Thanks, everyone... sorry it took so long to reply...
>
> Here's the code I came up with:
>
>      class test {
>      public:
>
>
>          test (void) {
>              pPointer = (char *) malloc (sizeof(char));

Two things:

why are you using malloc?

sizeof(char) is one by definition.

>          }
>
>          // Copy constructor
>          test (const test&  mytest) {
>
>               pPointer = (char *) malloc (sizeof(char));
>              *pPointer = *mytest.pPointer;
>
>          }
>
>          ~test () {
>              free (pPointer);
>          }
>
>          test foo (void) {
>              test myfoo;
>              return myfoo;
>          }
>
>          // = operator overload
>          test&  operator= (const test&  mytest) {
>
>              *pPointer = *mytest.pPointer;
>
>              return *this;
>
>          }
>
>          char* pPointer;
>      };
>
> Is it correct? (if we suppose that for the purpose of this test
> pPointer will only point to a single char)

Well it won't leak or double free, but what do you really want to do?

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6920

Fromnone <none9823@hotmail.com>
Date2011-06-19 22:53 -0700
Message-ID<7fea560f-f171-4fd7-ad1b-e4d97598b7e8@r2g2000vbj.googlegroups.com>
In reply to#6919
On Jun 20, 12:17 am, Ian Collins <ian-n...@hotmail.com> wrote:
> On 06/20/11 05:06 PM, none wrote:
>
> > Thanks, everyone... sorry it took so long to reply...
>
> > Here's the code I came up with:
>
> >      class test {
> >      public:
>
> >          test (void) {
> >              pPointer = (char *) malloc (sizeof(char));
>
> Two things:
>
> why are you using malloc?
>
> sizeof(char) is one by definition.
>
>
>
>
>
> >          }
>
> >          // Copy constructor
> >          test (const test&  mytest) {
>
> >               pPointer = (char *) malloc (sizeof(char));
> >              *pPointer = *mytest.pPointer;
>
> >          }
>
> >          ~test () {
> >              free (pPointer);
> >          }
>
> >          test foo (void) {
> >              test myfoo;
> >              return myfoo;
> >          }
>
> >          // = operator overload
> >          test&  operator= (const test&  mytest) {
>
> >              *pPointer = *mytest.pPointer;
>
> >              return *this;
>
> >          }
>
> >          char* pPointer;
> >      };
>
> > Is it correct? (if we suppose that for the purpose of this test
> > pPointer will only point to a single char)
>
> Well it won't leak or double free, but what do you really want to do?
>
> --
> Ian Collins

It's just a simplified test of a more complex code that didn't work,
I'm using just one char for the purpose of this test, but the final
code will handle a string.

[toc] | [prev] | [next] | [standalone]


#6921

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-06-20 06:06 +0000
Message-ID<4dfee374$0$2831$7b1e8fa0@news.nbl.fi>
In reply to#6920
none <none9823@hotmail.com> wrote:
> It's just a simplified test of a more complex code that didn't work,
> I'm using just one char for the purpose of this test, but the final
> code will handle a string.

  Any reason you can't use std::string or std::vector<char>? Using them
instead of allocating the string yourself will not only make your code
safer, it will also make it a lot simpler (you won't need to write a
copy constructor and assignment operator).

[toc] | [prev] | [next] | [standalone]


#6929

Fromnone <none9823@hotmail.com>
Date2011-06-20 00:35 -0700
Message-ID<3a723395-a6b3-4a14-8e70-6b0e9ded7b1e@m18g2000vbl.googlegroups.com>
In reply to#6921
On Jun 20, 1:06 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> none <none9...@hotmail.com> wrote:
> > It's just a simplified test of a more complex code that didn't work,
> > I'm using just one char for the purpose of this test, but the final
> > code will handle a string.
>
>   Any reason you can't use std::string or std::vector<char>? Using them
> instead of allocating the string yourself will not only make your code
> safer, it will also make it a lot simpler (you won't need to write a
> copy constructor and assignment operator).

In fact, I made a mistake when I said that the data was a string. The
string will be converted into BCD values, so the data pointer will
contain a arbitrary sized BCD number.

[toc] | [prev] | [next] | [standalone]


#6936

FromIan Collins <ian-news@hotmail.com>
Date2011-06-20 20:57 +1200
Message-ID<968gc6FsmeU4@mid.individual.net>
In reply to#6929
On 06/20/11 07:35 PM, none wrote:
> On Jun 20, 1:06 am, Juha Nieminen<nos...@thanks.invalid>  wrote:
>> none<none9...@hotmail.com>  wrote:
>>> It's just a simplified test of a more complex code that didn't work,
>>> I'm using just one char for the purpose of this test, but the final
>>> code will handle a string.
>>
>>    Any reason you can't use std::string or std::vector<char>? Using them
>> instead of allocating the string yourself will not only make your code
>> safer, it will also make it a lot simpler (you won't need to write a
>> copy constructor and assignment operator).
>
> In fact, I made a mistake when I said that the data was a string. The
> string will be converted into BCD values, so the data pointer will
> contain a arbitrary sized BCD number.

But why are you using malloc, or raw pointers?

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6966

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-06-20 18:25 +0000
Message-ID<4dff90b7$0$2849$7b1e8fa0@news.nbl.fi>
In reply to#6929
none <none9823@hotmail.com> wrote:
> In fact, I made a mistake when I said that the data was a string. The
> string will be converted into BCD values, so the data pointer will
> contain a arbitrary sized BCD number.

  That still doesn't explain why you can't use std::vector.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web