检测到glibc *** ./a.out:munmap_chunk():指针无效:

时间:2013-05-08 07:30:44

标签: c++ linux runtime-error delete-operator

有很多这样的问题,但在查看了一些案例之后,我想这个问题是特定于案例的,所以我发布了我的代码并指出了问题发生的地方你是否耐心阅读我的代码?

uniBTree.h

#ifndef uniBTree_H
#define uniBTree_H

#include "uniTreeNode.h"
#include <cassert>

template<class T>
class uniBTree {
    private:
      uniTreeNode<T> *root;
      int delete_helper(uniTreeNode<T> *);
      uniTreeNode<T> *insert_helper(uniTreeNode<T> *, const T);
      void in_print_helper(const uniTreeNode<T> *) const;
      void pre_print_helper(const uniTreeNode<T> *) const;
      void post_print_helper(const uniTreeNode<T> *) const;

    public:
      uniBTree(void);
      uniBTree(uniTreeNode<T> *r);
      ~uniBTree(void);
      void insert(const T i);
      void in_print(void) const;
      void pre_print(void) const;
      void post_print(void) const;
};

template<class T>
uniBTree<T>::uniBTree(void)
{
    root = NULL;
}

template<class T>
uniBTree<T>::uniBTree(uniTreeNode<T> *r)
{
    root = r;
}

template<class T>
int uniBTree<T>::delete_helper(uniTreeNode<T> *n)
{
    int count = 0;
    if (n == NULL)
        return 0;

    count += delete_helper(n->get_left());
    count += delete_helper(n->get_right());
    delete n;
    count++;
    return count;
}

template<class T>
uniBTree<T>::~uniBTree(void)
{
    int count = delete_helper(root);
    std::cout << "uniBTree<T>::~uniBTree<T>(void)\n";
    std::cout << count << " nodes deleted\n";
}

template<class T>
void uniBTree<T>::in_print() const
{
    in_print_helper(root);
}

template<class T>
void uniBTree<T>::pre_print() const
{
    pre_print_helper(root);
}

template<class T>
void uniBTree<T>::post_print() const
{
    post_print_helper(root);
}

template<class T>
void uniBTree<T>::in_print_helper(const uniTreeNode<T> *current) const
{
    if (current == NULL)
        return;
    in_print_helper(current->get_left());
    current->print();
    in_print_helper(current->get_right());
}

template<class T>
void uniBTree<T>::pre_print_helper(const uniTreeNode<T> *current) const
{
    if (current == NULL)
        return;
    current->print();
    pre_print_helper(current->get_left());
    pre_print_helper(current->get_right());
}

template<class T>
void uniBTree<T>::post_print_helper(const uniTreeNode<T> *current) const
{
    if (current == NULL)
        return;
    post_print_helper(current->get_left());
    post_print_helper(current->get_right());
    current->print();
}

template<class T>
void uniBTree<T>::insert(const T i)
{
    if (root == NULL)
        root = new uniTreeNode<T>(i, NULL, NULL);
    else
        insert_helper(root, i);
}

template<class T>
uniTreeNode<T> *uniBTree<T>::insert_helper(uniTreeNode<T> *current, const T i)
{
    if (current == NULL) {//this is will only dealed by attempting to visit leaves...
        //if root is null, it'll be handled in insert
        uniTreeNode<T> *child = new uniTreeNode<T>(i, NULL, NULL);
        assert(child != NULL);
        return(child);
    }
    if (i < current->get_data()) 
        current->set_left(insert_helper(current->get_left(), i));
    else 
        current->set_right(insert_helper(current->get_right(), i));
    return(current);
}

#endif

uniTreeNode.h

#ifndef uniTreeNode_H//for redefinition
#define uniTreeNode_H

#include <iostream>
//using namespace std; don't use using namespace xxx and include source file in .h file

template<typename T>
class uniTreeNode {
     private:
         T data;
         uniTreeNode<T> *left;
         uniTreeNode<T> *right;
     public:
         //uniTreeNode<T>(void);
         uniTreeNode(T d, uniTreeNode<T> *l, uniTreeNode<T> *r);
         T get_data(void) const;
         uniTreeNode<T> *get_left(void) const;
         uniTreeNode<T> *get_right(void) const;
         void set_left(uniTreeNode<T> *l);
         void set_right(uniTreeNode<T> *r);
         void print() const;
};

template<typename T>
uniTreeNode<T>::uniTreeNode/*remember syntax here*/
(T d , uniTreeNode<T> *l = NULL, uniTreeNode<T> *r = NULL)
{
     data = d;
     left = l;
     right = r;
}

template<typename T>
T uniTreeNode<T>::get_data(void) const
{
     return data;
}

template<typename T>
uniTreeNode<T> * uniTreeNode<T>::get_left(void) const
{
    return left;
}

template<typename T>
uniTreeNode<T> * uniTreeNode<T>::get_right(void) const
{
    return right;
}

template<typename T>
void uniTreeNode<T>::set_left(uniTreeNode<T> *l)
{
    left = l;
}

template<typename T>
void uniTreeNode<T>::set_right(uniTreeNode<T> *r)
{
    right = r;
}

template<typename T>
void uniTreeNode<T>::print() const
{
   std::cout << "data is " << data << std::endl;
}

#endif

date.h

#include <ostream>
class date{
    private:
            int y;
            int m;
            int d;
    public:
            date();//default constructor
            date(const long int);//used by cplr as convert constructor
            date(int, int , int);
            friend bool operator<(const date &d1, const date &d2);//d1 is for left-hand date
            friend bool operator>(const date &d1, const date &d2);
            bool operator==(date d);
            bool operator!=(date d);
            date &operator=(date d);
            friend std::ostream &operator<<(std::ostream &out, date d);
            friend std::istream &operator>>(std::istream &in, date d);
};

date.cc

#include <iostream>
#include <cstdio>
#include <time.h>
#include <cstring>
#include "date.h"

date::date(){
    y = m = d = 0;
}

date::date(int Y, int M, int D){
    y = Y;
    m = M;
    d = D;
}

date::date(const long int s){//#second since 1970/1/1 00:00:00
    struct tm *buf;
    buf = gmtime(&s);
    y = (buf->tm_year+1900);
    m = buf->tm_mon+1;
    d = buf->tm_mday;
}

bool operator<(const date &d1, const date &d2){
     bool result;//sizeof(bool) is 1
     if(d1.y < d2.y) result = true;
     else if(d1.y == d2.y){
           if(d1.m < d2.m) result = true;
           else if(d1.m == d2.m){
                   if(d1.d < d2.d) result = true;
                   else result = false;
           }
           else result = false;
     }
     else result = false;

     return result;
}

bool operator>(const date &d1, const date &d2){
    bool result;//sizeof(bool) is 1
    if(d1.y > d2.y) result = true;
    else if(d1.y == d2.y){
            if(d1.m > d2.m) result = true;
            else if(d1.m == d2.m){
                    if(d1.d > d2.d) result = true;
                    else result = false;
            }
            else result = false;
    }
    else result = false;

    return result;
}

bool date::operator==(date d){
    return (this->y==d.y && this->m==d.m && this->d==d.d); 
}

bool date::operator!=(date d){
    return (this->y!=d.y || this->m!=d.m || this->d!=d.d);
}

date &date::operator=(date d){
    this->y = d.y;
    this->m = d.m;
    this->d = d.d;
    return *this;
}

std::ostream &operator<<(std::ostream &out, date d){
    out << d.y << "/" << d.m << "/" << d.d << std::endl;
    return out;
}

std::istream &operator>>(std::istream &in, date d){
    in >> d.y >> d.m >> d.d ;
    return in;
}

主要功能

#include "uniBTree.h"
#include "date.h"
#include <cstdio>

int main(){
    date d1 = 100000000;//convert constructor
    uniTreeNode<date> node(d1, NULL, NULL);
    printf("%p %p\n", node.get_left(), node.get_right());
    std::cout << node.get_data() << std::endl;


    date d2 = 86401;
    date d3 = 200000000;

    uniBTree<date> btree(&node);

    return 0;
}

我测试过并发现其&node无效。我认为这是因为它试图在程序结束时“释放”btree并且遇到root时,因为它指向node,它无法执行好事。

我有两个问题:

  1. 如果像我所做的那样构建一个node,(uniTreeNode<date> node(xxx, xxx, xxx);)是该程序编辑的“NEW”对象吗?
  2. 对于uniTreeNode<T>类模板,我没有编写它的析构函数!那么,就像我上面所说的那样,当btree的根指向的节点被释放时,是否存在所谓的“默认析构函数”?它叫这里吗?最重要的是,程序使用“DELETE”吗?
  3. 如果上述两个问题中的一个不是,那么问题出现的原因是什么?

    编辑:现在显示问题了,但是如何调整我的代码来解决这个问题呢?任何一个想法?

    编辑:只需修改如下:

    uniTreeNode<date> *nodeptr = new uniTreeNode<date>(d1, NULL, NULL);
    

    P.S。如果没有间接使用指针来引用我们的btree根(因此使用new),则不使用new,并且不应使用delete;通过这个选择,uniTreenode的delete_helper应该使用:

    if(n != root){
        delete n;
        count++;
    }
    

    但这并没有解决问题...... 最终的问题是:

    "can we release object without using delete(because it isn't obtained from newing) in c++?"
    

    REPLY:

    我的“释放”/“已分配”实际上是关于内存的,而没有说明它是如何完成的...但无论如何这都是一个大问题

    你说“你可以这样做,但它几乎总是错误的答案”;你的意思是我应该使用DELETE而不是直接调用析构函数?(实际上这似乎不合适) - &gt;请在这里证明

    顺便说一下,对于那些我刚接触过的实例,如果我想释放它们,是否有必要通过声明删除它们?或者它们也会像那些自动变量实例一样处理?(当超出范围时,通过编译器返回) - &gt;如果需要,请更正以上内容

    另一个问题:对于那些自动实例,我是否可以用任何现有的语句来做事情,比如DELETE做的事情?或者,如果我愿意,我只能打电话给析构函数?

2 个答案:

答案 0 :(得分:1)

回答你的问题:

  1. 不,它在编译时在堆栈上分配了内存,只是运行了构造函数。
  2. 您无法删除未使用new分配的指针。当对象节点在main()中完成时,编译器会对uniTreeNode插入对析构函数的调用(默认是否为默认值)。
  3. 所以推测,你不能对未使用new分配的指针使用delete。

    最简单的解决方法是使用new分配节点:

    uniTreeNode<date>* node = new uniTreeNode<date>(d1);
    
    uniBTree<date> btree(node);
    

答案 1 :(得分:0)

学习使用valgrind

它会立即告诉您问题是什么,您正在删除uniBTree析构函数中的堆栈对象

==23648== Invalid free() / delete / delete[] / realloc()
==23648==    at 0x4A0736C: operator delete(void*) (vg_replace_malloc.c:480)
==23648==    by 0x400D78: uniBTree<date>::delete_helper(uniTreeNode<date>*) (uniBTree.h:48)
==23648==    by 0x400CD5: uniBTree<date>::~uniBTree() (uniBTree.h:56)
==23648==    by 0x400B91: main (main.cc:17)
==23648==  Address 0x7fefffab0 is on thread 1's stack
==23648== 

析构函数调用delete&node不是new创建的(你可以告诉它,因为你没有写new!)