帮助!!! ---“表达式:_CrtIsValidHeapPointer(pUserData)" [英] Help!!! --- "Expression: _CrtIsValidHeapPointer(pUserData)"

查看:97
本文介绍了帮助!!! ---“表达式:_CrtIsValidHeapPointer(pUserData)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:

这张图片有什么问题--表达式:_CrtIsValidHeapPointer(pUserData)".

在类中调试时捕获了异常,代码如下:

Hi, guys:

What''s wrong with this picture -- -- "Expression: _CrtIsValidHeapPointer(pUserData)".

The exception was caught when I was debugging in a class, codes like below:

class Myclass
{
   Myclass(){
   m_data = new char[50];
   m_data = "You are someone!!";
   }
   ~Myclass(){delete []m_data;}
   
   void SetStr(char *para)
   {
        (NULL == para)
            return;
        if(m_data != para)
        {
            delete []m_data; // The exception was caught here, debugging stopped.
            m_data = NULL;
            m_data = new char[strlen(para)+1];
            strcpy(m_data,para);
        }

   }
private:
   char *m_data;
}



int main(int argc, char* argv[])
{
   Myclass *pMyclass = new Myclass();
   pMyclass->SetStr("Weak !!! ");
   delete  pMyclass;
}





我的项目比这里介绍的要复杂,但是有关"m_data"用法的上下文几乎相同.

我试图释放m_data的内存删除[] m_data"时出了什么问题,该如何避免呢?





My project was more complicated than what I presented here, but the context about ''m_data'' usage was almost the same.

What''s the problem while I was trying to release m_data''s memory,"delete []m_data", how can I do to avoid?

推荐答案

第一次进入时,指针不是指向堆内存:它是指向静态内存!
The first time you go in, the pointer is not to heap memory: it is to static memory!
m_data = "You are someone!!";


使用m_data的方式是胡说八道.
查看MyClass::MyClass构造函数,首先从堆中分配50个字符,将地址分配给m_data,然后将常量"You are someone"的地址分配给m_data.从这一点开始,您分配的50个字符将泄漏(并且不可访问),其中m_data指向常量文字,您稍后尝试在析构函数和SetStr中删除它们.

在MyClass :: SetStr中有一个(NULL == para)return;.如果将if放在前面(这是拼写错误!!?),这会更有意义.

实际上,字符串文字和字符数组无法按照您的想法工作.
特别是
The way you work with m_data is a nonsense.
Looking the MyClass::MyClass constructor, you first allocate 50 chars from the heap, assigning the address to m_data, then you assign to m_data the address of the literal "You are someone". Since this point, the 50 chars you allocate are leaked (and unreachable) with m_data pointing to a constant literal, that you try do delete later on, in the destructor and in SetStr.

In MyClass::SetStr there is a (NULL==para) return;. It will make more sense if you place an if in front (is it a typo?!?).

In practice, string literal and character arrays don''t work the way you think.
In particular
m_data = "You are someone!!";


将字符串复制到m_data指向的数组中.

要正确复制,您应该使用strcpy 之类的库函数或类似的函数,其工作原理如下:


doesn''t copy the string into the array pointed by m_data.

To properly copy you should use library fuctions like strcpy or similar, the does a work like this:

void copy_string(char* dest, unsigned dest_sz, char* src)
{
    unsigned i;
    for(i=0; i<dest_sz-1 && src[i]; ++i)
        dest[i] = src[i];
    dest[i]='\0';
}


这篇关于帮助!!! ---“表达式:_CrtIsValidHeapPointer(pUserData)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆