c++ - Can't delete temporary object -


i'm resizing , array of objects. made temp object when don't delete valgrind shows memory leaks , error. deleting causes segfault. wondering valgrind complaining about...

void obj::resize() {   obj *temp = new obj[size * 2];   //<-- line 92   (int = 0; < size; i++)     temp[i] = objarray[i];   delete [] objarray;   objarray = temp;   size *= 2;   //delete temp;   //<-- causes segfault   //delete [] temp;  // segfaults, tried them both in case :\ } 

here's valgrind report:

==9292== heap summary: ==9292==     in use @ exit: 21,484 bytes in 799 blocks ==9292==   total heap usage: 3,528 allocs, 2,729 frees, 91,789 bytes allocated ==9292== ==9292== 21,484 (2,644 direct, 18,840 indirect) bytes in 1 blocks lost in loss record 4 of 4 ==9292==    @ 0x4008409: operator new[](unsigned int) (vg_replace_malloc.c:357) ==9292==    0x804ac7e: myclass::resize() (file.cpp:92) ==9292==    0x804ac34: myclass::add(int, int) (file.cpp:82) ==9292==    0x804aae6: getline(std::istream&, myclass&) (file.cpp:66) ==9292==    0x8049772: main (otherfile.cpp:39) ==9292== ==9292== leak summary: ==9292==    lost: 2,644 bytes in 1 blocks ==9292==    indirectly lost: 18,840 bytes in 798 blocks ==9292==      possibly lost: 0 bytes in 0 blocks ==9292==    still reachable: 0 bytes in 0 blocks ==9292==         suppressed: 0 bytes in 0 blocks ==9292== ==9292== counts of detected , suppressed errors, rerun with: -v ==9292== error summary: 1 errors 1 contexts (suppressed: 0 0) 

i'm not gdb, got backtrace:

    (gdb) run starting program:   program received signal sigsegv, segmentation fault. 0x46ed40e3 in free () /lib/libc.so.6 missing separate debuginfos, use: debuginfo-install glibc-2.15-58.fc17.i686 libgcc-4.7.2-2.fc17.i686 libstdc++-4.7.2-2.fc17.i686 (gdb) backtrace #0  0x46ed40e3 in free () /lib/libc.so.6 #1  0x4742dba0 in operator delete(void*) () /lib/libstdc++.so.6 #2  0x0804ad68 in myclass::resize (this=0xbffff28c) @ file.cpp:98 #3  0x0804ac35 in myclass::add (this=0xbffff28c, month=10, day=31)     @ file.cpp:82 #4  0x0804aae7 in getline (input=..., a=...) @ file.cpp:66 #5  0x08049773 in main (argc=1, argv=0xbffff344) @ otherfile.cpp:39 (gdb) 

i think deleting bad, because should leave pointer dangling, doesn't surprise me segfault. still, why cause memory problems? ideas appreciated.

indeed, can't delete there since you've assigned objarray used later.

most likely, you're not deleting objarray in destructor; or other function reassigning without deleting old array first.

i use std::vector rather hand-crafted array, take care of deallocation me.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -