删除指针问题

时间:2011-04-29 21:51:58

标签: c++ pointers memory-management

我无法删除我创建的指针。该程序创建一个指向线程的双指针。然后它动态创建线程。最后它会删除它们但我得到一个glibc错误。它使用boost来创建线程。真正令人费解的是,我以同样的方式删除了一个类似的双指针并且执行得很好。问题出现在/*clean up*/标题下的代码块的末尾:

boost :: thread** thrds;

//create threads and bind to p_variantforloop_t
thrds = new boost::thread*[numThreads];

for (int i = 1; i <= numThreads; i++)
    thrds[i] = new boost::thread(boost::bind(&p_variantforloop_t, 
        E, A, D, (i*n-n)/i ,(i*n)/n, numThreads, n)); 

/* join threads */
for (int i = 0; i < numThreads; i++)
    thrds[i]->join();

/* cleanup */
for (int i = 0; i < numThreads; i++)
    delete thrds[i];
delete[] thrds;

错误是:

  

*** glibc detected *** ./hw9: munmap_chunk(): invalid pointer: 0x0957d480 ***
  ======= Backtrace: =========
  /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x264591]
  /lib/tls/i686/cmov/libc.so.6(+0x6c80e)[0x26580e]
  /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x529741]
  ./hw9[0x804a0d1]
  /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x20fbd6]
  ./hw9[0x8049871]
  ======= Memory map: ========
  001f9000-0034c000 r-xp 00000000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
  0034c000-0034d000 ---p 00153000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
  0034d000-0034f000 r--p 00153000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
  0034f000-00350000 rw-p 00155000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
  00350000-00353000 rw-p 00000000 00:00 0
  0046e000-00557000 r-xp 00000000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
  00557000-00558000 ---p 000e9000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
  00558000-0055c000 r--p 000e9000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
  0055c000-0055d000 rw-p 000ed000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
  0055d000-00564000 rw-p 00000000 00:00 0
  005ad000-005d1000 r-xp 00000000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
  005d1000-005d2000 r--p 00023000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
  005d2000-005d3000 rw-p 00024000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
  00950000-00965000 r-xp 00000000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
  00965000-00966000 r--p 00014000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
  00966000-00967000 rw-p 00015000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
  00967000-00969000 rw-p 00000000 00:00 0
  00a67000-00a7a000 r-xp 00000000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
  00a7a000-00a7b000 r--p 00013000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
  00a7b000-00a7c000 rw-p 00014000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
  00bc7000-00be2000 r-xp 00000000 08:02 1128318 /lib/ld-2.11.1.so
  00be2000-00be3000 r--p 0001a000 08:02 1128318 /lib/ld-2.11.1.so
  00be3000-00be4000 rw-p 0001b000 08:02 1128318 /lib/ld-2.11.1.so
  00c34000-00c3b000 r-xp 00000000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
  00c3b000-00c3c000 r--p 00006000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
  00c3c000-00c3d000 rw-p 00007000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
  00ccf000-00cd0000 r-xp 00000000 00:00 0 [vdso]
  00e85000-00ea2000 r-xp 00000000 08:02 1128359 /lib/libgcc_s.so.1
  00ea2000-00ea3000 r--p 0001c000 08:02 1128359 /lib/libgcc_s.so.1
  00ea3000-00ea4000 rw-p 0001d000 08:02 1128359 /lib/libgcc_s.so.1
  08048000-0804e000 r-xp 00000000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
  0804e000-0804f000 r--p 00005000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
  0804f000-08050000 rw-p 00006000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
  0957d000-0959e000 rw-p 00000000 00:00 0 [heap]
  b68bf000-b68c0000 ---p 00000000 00:00 0
  b68c0000-b70c0000 rw-p 00000000 00:00 0
  b70c0000-b70c1000 ---p 00000000 00:00 0
  b70c1000-b78c4000 rw-p 00000000 00:00 0
  b78e8000-b78eb000 rw-p 00000000 00:00 0
  bfc07000-bfc1c000 rw-p 00000000 00:00 0 [stack]
  Aborted

1 个答案:

答案 0 :(得分:3)

创建线程对象时省略数组的第一个元素。它保持未初始化的指针值,导致稍后无效的指针错误。

还有一个问题就是在数组的最后一个元素之后立即写入内存,你也会在有问题的循环(即代码中的第一个for)中进行写入。