簡體   English   中英

glibc檢測到*** ./a.out:munmap_chunk():無效的指針:

[英]glibc detected *** ./a.out: munmap_chunk(): invalid pointer:

有很多類似這樣的問題,但是在研究了一些案例之后,我想這個問題是針對特定案例的,因此我發布了我的代碼,並指出問題出在哪里,您可能會耐心閱讀我的代碼嗎?

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 ,並且遇到根時,因為它指向node ,所以不能執行任何操作。

我有兩個問題:

  1. 如果像我一樣構造一個node ,( uniTreeNode<date> node(xxx, xxx, xxx); )是程序“新建”了對象嗎?
  2. 對於uniTreeNode<T>類模板,我沒有編寫其析構函數! 因此,就像我上面所說的那樣,當要釋放由btree的根指向的node時,是否存在所謂的“默認析構函數”? 叫這里嗎? 最重要的是,程序是否使用了“刪除”?

如果以上兩個問題之一不存在,為什么會出現問題?

編輯:現在顯示問題,但是如何調整代碼以解決此問題? 任何一個想法嗎?

編輯:像這樣修改:

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

ps如果不間接使用指針來引用我們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所做的事情)? 或者,如果願意,我只能調用析構函數?

回答您的問題:

  1. 不,它在編譯時在堆棧上分配了內存,然后在其上運行了構造函數。
  2. 您不能刪除未使用new分配的指針。 當對象節點在main()中完成時,編譯器將插入對uniTreeNode的析構函數的調用(默認設置或不設置)。

因此,您不能在未使用new分配的指針上使用delete。

最簡單的解決方法是使用new分配節點:

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

uniBTree<date> btree(node);

學習使用valgrind

它立即告訴您問題出在uniBTree ,您正在刪除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 !)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM