【问题标题】:glibc detected *** ./a.out: munmap_chunk(): invalid pointer:glibc 检测到 *** ./a.out: munmap_chunk(): 无效指针:
【发布时间】:2013-05-08 07:30:44
【问题描述】:

这样的问题有很多,但是在查看了一些案例之后,我想这个问题是针对特定案例的,所以我发布了我的代码并指出了问题发生在哪里,您可以耐心阅读我的代码吗?

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

日期.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;
}

我测试发现它的&amp;node是无效的。我认为是因为它试图在程序结束和遇到root时“释放”btree,因为它指向node,所以它不能执行好事情。

我有两个问题:

  1. 如果像我做的那样构造一个node,(uniTreeNode&lt;date&gt; node(xxx, xxx, xxx);)是程序“新”的对象吗?
  2. 对于uniTreeNode&lt;T&gt; 类模板,我没有写它的析构函数!!那么,就像我上面所说的,当btree的根指向的节点要被释放时,是否有所谓的“默认析构函数”?它在这里被调用吗?最重要的是,程序是否使用了“DELETE”?

如果以上两个问题之一是否定的,那是问题出现的原因吗?

编辑:现在问题出现了,但我该如何调整我的代码来解决这个问题?有人知道吗?

编辑:只需像这样修改:

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

附言如果不是间接使用指针来引用我们的 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++?"

回复:

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

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

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

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

【问题讨论】:

  • “释放”是什么意思?释放 new 分配的内存是我常用的。也许你的意思是运行析构函数?你可以这样做,但它几乎总是错误的答案。未分配的对象(例如使用 new)只能有一个“所有者”(在其中声明它的函数或对象),因此编译器会在适合此类对象的情况下自动添加对析构函数的调用。

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


【解决方案1】:

回答您的问题:

  1. 不,它在编译时在堆栈上分配内存,然后在上面运行构造函数。
  2. 您不能删除未使用 new 分配的指针。当对象节点在 main() 中完成时,编译器会插入对 uniTreeNode 析构函数的调用(默认与否)。

因此推测,您不能对未使用 new 分配的指针使用 delete。

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

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

uniBTree<date> btree(node);

【讨论】:

    【解决方案2】:

    学习使用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,但&amp;node不是由new创建的(你可以看出来,因为你没有写new!)

    【讨论】:

    • thx 其实我知道有这样的调试工具
    • 非常感谢,其实我知道 valgrind,但我不熟悉使用它,我会学习它!
    猜你喜欢
    • 1970-01-01
    • 2013-08-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多