_CRTLISVALIDHEAPPOINTER在破坏过程中的错误
_CrtlIsValidHeapPointer error during destruction
我,当我的显示对象t_display
被解构时,会遇到_CrtlIsValidHeapPointer(block)
错误。这发生在Animation.cpp中的while循环之后,用于获取用户输入并进行多个显示。
我知道这是因为我为char * p_name
分配内存,然后将该指针存储在对象中,但我不确定如何解决它。我必须使用char *作为显示对象名称,所以这必须意味着我必须为其分配内存。
我认为可能有两个问题之一。1)我正在为char *分配错误或错误地复制字符串的内存2)我写了错误的distuructor
在这两种情况下,我都不确定如何解决我遇到的错误,希望您可以指向正确的方向。
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 AnimationnPlease 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;
}
它将丢弃该错误。
在您的复制ctor中,您将名称的值分配给新对象,因此DTOR将对原始对象和复制对象都运行,但每个对象都会尝试释放指针。
在您的CTOR中分配新的缓冲区并复制内容。最好是简单地使用std :: string作为显示成员,那么您唯一需要关注的缓冲区就是您用来查询操作系统的一个。
相关文章:
- 如何解决导入dll过程中Viual Studio中的并排配置错误
- 如何确保不会在 CI 过程中生成错误的代码?
- 为什么在编译过程中尽管有 -I(破折号大写 i)标志,但仍会出现未定义的引用错误?
- RAII获取在破坏过程中捕获的错误的方法
- 编译过程中有许多错误opencv_contrib
- 生成过程中的 Clion 编译器错误
- 我的代码在执行过程中运行良好.但是当我去提交它的显示分割错误时
- 在输入结构对象的输入过程中运行时间错误
- _CRTLISVALIDHEAPPOINTER在破坏过程中的错误
- C 在结构初始化过程中带有内部阵列的奇怪错误
- C++ 'stod()'不起作用,'strtod()'编译过程中抛出错误
- 如何获取有关protobuf解析过程中生成的错误的更多详细信息?(C++)
- 在新汇编过程中浏览以前的补偿错误
- 在包含新CPP文件后的链接过程中,MSVC 错误
- 如何使用"python setup.py build"解决C++/C编译过程中的"conflicting types"错误?
- 校准过程中出现OpencCV错误
- 如何分配一个当前在分配过程中抛出 std::bad_alloc 错误的大量布尔数组
- C++1z-如果在模板展开过程中出现条件,则引发编译错误
- 编译过程中关于 boost 静态库的链接错误"undefined reference"
- 不了解 gSOAP 序列化过程中的错误