C++ 尝试使用析构函数删除动态分配矩阵时出错

C++ 尝试使用析构函数删除动态分配矩阵时出错,c++,C++,正如标题所述,我试图使用析构函数删除动态分配矩阵,但出现以下错误: 在oop.exe中的0x78D8DB1B(ucrtbased.dll)处引发异常:0xC0000005: 访问冲突读取位置0xDDCD 下面是我尝试运行的代码 #include <iostream> using namespace std; template<class T> class Matrice { private: int ma

正如标题所述,我试图使用析构函数删除动态分配矩阵,但出现以下错误:

在oop.exe中的0x78D8DB1B(ucrtbased.dll)处引发异常:0xC0000005: 访问冲突读取位置0xDDCD

下面是我尝试运行的代码

    #include <iostream>
    using namespace std;

     template<class T>
    class Matrice
    {
    private:
        int marime;
        T** matrice;
    public:
        Matrice(int marime);
        ~Matrice();
        friend istream& operator>>(istream& in,Matrice<T>& mat) {
            for (int i = 0; i < mat.marime; i++) {
                for (int j = 0; j < mat.marime; j++) {
                    cout << "Matrice[" << i << "][" << j << "]: ";
                    in >> mat.matrice[i][j];
                }
            }
            return in;
        }
        friend ostream& operator<<(ostream& out,Matrice<T> mat) {
            for (int i = 0; i < mat.marime; i++) {
                cout << endl;
                for (int j = 0; j < mat.marime; j++) {
                    out << mat.matrice[i][j]<<" ";
                }
            }
            return out;
        }
    };

    template<class T>
    Matrice<T>::Matrice(int marime) {
        this->marime = marime;
        matrice = new T * [marime];
        for (int i = 0; i < marime; i++) {
            matrice[i] = new T[marime];
        }
    }
    template<class T>
    Matrice<T>::~Matrice() {
        for (int i = 0; i < marime; i++) {
            delete[] matrice[i]; //Here is where i get the error.
        }
        delete[] matrice;
    }

int main()
{
    Matrice<int> test(3);
    cin >> test;
    cout << test;
}
#包括
使用名称空间std;
模板
类矩阵
{
私人:
马吕米;
T**矩阵;
公众:
Matrice(int-marime);
~Matrice();
friend istream和operator>>(istream和in、Matrice和mat){
for(int i=0;iCUT< p>内存地址<代码> 0xDDDDDDC是免费bug后使用的一个可能的标志,因为VisualC++调试用该内存模式构建了所有空闲内存。我在Linux上使用ASAN编译了程序(<代码> CLAN++Matr.CC-GOG-FSAITiZZ=地址< /代码>),并能够用以下StAcTrace:/P>复制您的问题。
==6670==ERROR: AddressSanitizer: heap-use-after-free on address 0x603000000010 at pc 0x0000004c9a76 bp 0x7fffdcd001b0 sp 0x7fffdcd001a8
READ of size 8 at 0x603000000010 thread T0
    #0 0x4c9a75 in Matrice<int>::~Matrice() /tmp/z.cc:44:22
    #1 0x4c93c9 in main /tmp/z.cc:54:1
    #2 0x7f34a76370b2 in __libc_start_main /build/glibc-YYA7BZ/glibc-2.31/csu/../csu/libc-start.c:308:16
    #3 0x41f3cd in _start (/tmp/a.out+0x41f3cd)

0x603000000010 is located 0 bytes inside of 24-byte region [0x603000000010,0x603000000028)
freed by thread T0 here:
    #0 0x4c736d in operator delete[](void*) (/tmp/a.out+0x4c736d)
    #1 0x4c93c1 in main /tmp/z.cc:53:5
    #2 0x7f34a76370b2 in __libc_start_main /build/glibc-YYA7BZ/glibc-2.31/csu/../csu/libc-start.c:308:16

previously allocated by thread T0 here:
    #0 0x4c6b1d in operator new[](unsigned long) (/tmp/a.out+0x4c6b1d)
    #1 0x4c9536 in Matrice<int>::Matrice(int) /tmp/z.cc:36:19
==6670==错误:地址消毒器:在pc 0x0000004c9a76 bp 0x7fffdcd001b0 sp 0x7fffdcd001a8上的地址0x603000000010上释放后使用堆
在0x603000000010螺纹T0处读取尺寸8
#矩阵中的0 0x4c9a75::~Matrice()/tmp/z.cc:44:22
#main/tmp/z.cc中的1 0x4c93c9:54:1
#主/build/glibc-YYA7BZ/glibc-2.31/csu/。/csu/libc start.c:308:16中的0x7f34a76370b2
#3 0x41f3cd输入/启动(/tmp/a.out+0x41f3cd)
0x603000000010位于24字节区域[0x603000000010,0x603000000028]内0字节处
线程T0在此释放:
#运算符delete[](void*)(/tmp/a.out+0x4c736d)中的0 0x4c736d
#主/tmp/z.cc中的1 0x4c93c1:53:5
#主/build/glibc-YYA7BZ/glibc-2.31/csu/。/csu/libc start.c:308:16中的0x7f34a76370b2
在此之前由线程T0分配:
#0 0x4c6b1d in运算符新[](无符号长)(/tmp/a.out+0x4c6b1d)
#矩阵中的1 0x4c9536::矩阵(int)/tmp/z.cc:36:19

在第53行调用的析构函数释放某些资源后,析构函数在第44行第22列(
delete[]matrice[i];
)读取该资源(
cout
接受类型为
Matrice&
的参数,这很好,而
运算符打开编译器警告。修复这些警告。添加
Matrice(Matrice const&)=delete;
为什么不使用
std::vector
?如果不允许,编写自己的(简化)它的版本。
dddddd cd
可能意味着内存已被释放。哇,感谢您对发生此错误的原因所作的深入解释。