销毁期间_CrtlIsValidHeapPointer错误

时间:2017-12-10 21:06:34

标签: c++ string memory-leaks char destructor

我正在解构我的显示对象_CrtlIsValidHeapPointer(block)时出现t_display错误。这发生在Animation.cpp中的while循环之后,用于获取用户输入并进行多次显示。

我知道这是因为我为char * p_name分配内存,并将该指针存储在对象中,但我不确定如何绕过它。我必须使用char *作为显示对象名称,所以这必须意味着我必须为它分配内存。

我认为可能存在两个问题之一。 1)我为char *分配内存错误,或者错误地复制了字符串 2)我写了析构函数错误

在这两种情况下,我都不确定如何解决这个错误,我希望你能指出我正确的方向。

Animation.cpp

#include <crtdbg.h>
#include <iostream>
#include <string>
#include <vector>
#include <forward_list>
using namespace std;

#include "Display.h"
#include "Frame.h"
#include "Animation.h"


void Animation::InsertFrame() {

    int numDisplays; //for user input of display number
    vector <Display>v; //vector for containing display objects
    int p_x; //will contain user input for pixel_x
    int p_y; //will contain user input for pixel_y
    int p_duration; //will contain user input for duration
    char * p_name; //temp string to contain user input for name

   //will contain p_name to be passed to display constructor
    string frameName; //contains user input for the frame name
    int q = 0; //used to count the diplay #

    //begin reading user input
    cout << "Insert a Frame in the Animation\nPlease enter the Frame filename: " ;
    cin >> frameName;
    cout << "Entering the Frame Displays (the sets of dimensions and durations) " << endl;
    cout << "Please enter the number of Displays: " ;
    cin >> numDisplays;
    string d_name;

    //display creation loop for # of displays entered
    while (numDisplays > 0) {
        //char * name=nullptr;
        cout << "Please enter pixel x for Display #"<<q<<" pixel_x:";
        cin >> p_x;
        cout << "Please enter pixel y for Display #"<<q<<" pixel_y:" ;
        cin >> p_y;
        cout << "Please enter the duration sec for this Display: " ;
        cin >> p_duration;
        cout << "Please enter the name for this Display: " ;
        //cin >> p_name;
        cin >> d_name;
        //p_name = new char[strlen(name)];
        p_name = new char[d_name.length() + 1]; //allocate for the size of the name entered
        strcpy(p_name, d_name.c_str()); //copy string to char []
        Display t_display =  Display(p_x, p_y, p_duration, p_name); //make a new display with the user input values
        v.push_back(t_display); //pushing onto the vector
        numDisplays--;
        q++;

    }

Display.h

// Display.h
#pragma once

class Display
{
    int pixel_x;
    int pixel_y;
    int duration;
    char* name;
public:
    Display(int x, int y, int duration, char* name);
    Display(const Display&);
    ~Display();
    friend ostream& operator<<(ostream&, Display&);
};

Display.cpp

#include <crtdbg.h>
#include <iostream>
#include <string>
#include <vector>
#include <forward_list>
using namespace std;
#include "Display.h"

Display::Display(int x, int y, int d, char* n):pixel_x(x), pixel_y(y), duration(d), name(n) {
}
Display::Display(const Display& p) {
    //copy values from p
    pixel_x = p.pixel_x;
    pixel_y = p.pixel_y;
    duration = p.duration;
    name = p.name;

}
Display::~Display() {

}

该程序在没有析构函数的情况下工作,但当然有内存泄漏,这是不可接受的。当我添加一个简单的析构函数,如:

if(name){
 delete[] name;
}

它会抛出这个错误。

1 个答案:

答案 0 :(得分:0)

在你的copy-ctor中你将name的值赋给新对象,所以dtor将为原始对象和复制对象运行,但每个都会尝试释放指针。

在你的ctor中分配一个新缓冲区并复制内容。更好的方法是简单地使用std :: string作为Display成员,然后您需要关注的唯一缓冲区是用于查询操作系统的缓冲区。