C++ 中的析构函数不起作用

Destructor in C++ doesn't work

谁能告诉我析构函数有什么问题吗?如果我删除它一切正常。

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <conio.h>

class DynString
{
private :

    char *string;
    int size;

public :

    DynString(const char *tab)
        {
                string = new char[strlen(tab)];
                size = strlen(tab);
                if (string != NULL)
                {
                    strcpy(this->string, tab);
                }
        }

        DynString(const DynString& s)
        {
            string = new char[s.size];
            if (string != NULL)
            {

                    strcpy(string, s.string);
                    size = s.size;
            }
            else size = 0;


    }

    int Size() const
    {
            return size;
    }

    const char* CStr()
    {
            return string;
    }

    DynString& operator +=(const char* tab)
    {
            char *bufor = new char[size + strlen(tab)];
            if (bufor != NULL)
            {

                    strcpy(bufor, string);
                    strncat(bufor, tab, strlen(tab));
                    string = new char[strlen(bufor) + sizeof(char)];
            }

            if (string != NULL)
            {

                    strcpy(string, bufor);
                    size = strlen(string);
            }
            return *this;
    }

    DynString& operator !()
    {
            unsigned int size = strlen(string);
            for (unsigned int i = 0; i < size; i++)
            {
                    if (string[i] >= 97 && string[i] <= 122)
                    {
                            string[i] -= 32;
                    }
                    else if (string[i] >= 65 && string[i] <= 90)
                    {
                            string[i] += 32;
                    }
            }
            return *this;
    }
    ~DynString();


};
DynString::~DynString()
{
        if (size == 0)
        {
                delete string;
        }
        else
        {
                delete[] string;
        }
}

int _tmain(int argc, _TCHAR* argv[])
{
        DynString string("Test words.");
        printf("String %s\n", string.CStr());
        printf("Characters: %i\n", string.Size());

        DynString copy = string;
        printf("Copy %s\n", copy.CStr());
        printf("Characters: %i\n", copy.Size());

        copy += " - first fragment -";
        copy += " second fragment.";

        printf("Copy %s\n", string.CStr());
        printf("Characters: %i\n", string.Size());
        printf("Copy %s\n", copy.CStr());
        printf("Characters: %i\n", copy.Size());

        !string;
        printf("String %s\n", string.CStr());
        printf("\nEnd.");
        _getch();
        return 0;
}

你的析构函数应该是:

if(string!=NULL)
{
     delete[] string;
}

或者只是

delete[] string;

(假设 delete[]ing 一个 NULL 指针是安全的)。

记住:用 new[] 分配的所有东西都必须用 delete[] 销毁;用 new 分配的所有东西都必须用 delete.

销毁

编辑:

此外,您应该分配 stringstrlen(tab)+1 space,以容纳终止空字节(对于复制构造函数,这意味着 s.size+1)。

您应该按以下方式定义析构函数:

DynString::~DynString()
{
    delete[] string;
    string = nullptr;
}

但是,我不会坚持手动内存管理,而是使用智能指针。