C++ delete[] calls destructor

EDITED : added constructor and destructor.

EDIT : this is the leak :

c:userssijaandesktop1starray.cpp(61) : {148} normal block at 0x007C0910, 40 bytes long.
 Data: <        h |     > C8 00 00 00 02 00 00 00 68 09 7C 00 CD CD CD CD 
c:userssijaandesktop1starray.cpp(43) : {145} normal block at 0x007C04B0, 40 bytes long.
 Data: <d         {     > 64 00 00 00 01 00 00 00 A8 E2 7B 00 CD CD CD CD 
c:userssijaandesktop1starray.cpp(24) : {143} normal block at 0x007C0150, 816 bytes long.
 Data: <    X {         > 80 00 00 00 58 E1 7B 00 CE CD CD CD CE CD CD CD 
Object dump complete.

I'm trying to create a new object after every call for this function :

    bool StArray::addCS_Course(int StudentID, int CourseID, char * CourseName, int HwNum, double HwWeigh, int Flag, char * BookName)
{
    for (int i = 0; i < MAX_STUDENT_NUM; i++) {
        if (StArray_[i] == NULL) continue;
        if (StArray_[i]->getID() == StudentID) {

            CS_Course* New_CS = new CS_Course(CourseID, CourseName, HwNum, HwWeigh, Flag, BookName);

            StArray_[i]->addCS_Course(New_CS);
            return true;
        }

    }
        return false;
}

should I use new to do this? or the constructor itself makes an object to be used outside this function?

Here's the class declaration :

class CS_Course : public Course {

public:
    /*Interface functions*/
    /* *****************************************************
    Function: CS_Course
    Abstract: constructor
    Parameters: 
        Course_Id : course number
        Course_Name : course name
        Hw_Num : homeworks number
        Hw_Weigh : the weigh of the homeworks
    Return Value: 
    ***************************************************** */
    CS_Course(int Course_ID, char* Course_Name, int Hw_Num, double Hw_Weigh, int flag, char* BookName); // constructor
    ~CS_Course();

private:
    int flag_;
char* BookName_;

I'm asking this because of that when I'm deleting the CourseName string and the BookName, I'' still have a new allocated object which has nothing. Then Im doing this : delete[] pCSC[j]; pCSC has pointers to the CS_Course. But I get an error because this delete calls the CS_Course destructor which trys to destroy the CourseName which I already destroyed.

This is the constructor :

CS_Course::CS_Course(int Course_ID, char * Course_Name, int Hw_Num, double Hw_Weigh, int flag, char * BookName)
    :Course(Course_ID, Course_Name, Hw_Num, Hw_Weigh), flag_(flag)
{
    BookName_ = new char[strlen(BookName) + 1];
    strcpy(BookName_, BookName);
}

and this is the destructor :

CS_Course::~CS_Course() 
{
    delete[] BookName_;
}

The new here is necessary, since you are passing a pointer to the newly created object. If you were creating a local variable and passing it, it wont be available outside this function.

Also, make sure to delete the objects when you no longer needed (ie if you remove it from the array). otherwise it will cause a memory leak.


You're used to C and switch to C++ now, aren't you?

EDIT:

You often, you don't have to allocate something at all:

class CS_Course : public Course {
public:
    CS_Course(int Course_ID, char* Course_Name, int Hw_Num, double Hw_Weigh, int flag,char* BookName) 
    :Course(Course_ID, Course_Name, Hw_Num, Hw_Weigh), flag_(flag), BookName(BooName)
    {
    }
private:
    int flag_;
    std::string BookName_;
};

Allocating data structures in functions is something you can't avoid in C, but in C++! Never use raw new s in modern C++ code!

Use the new smart pointers like std::unique_ptr and std::shared_ptr .

They allow you to do exactly what you wan't without any problems about double deleting.

Also try to not use the built in C data types. Use std::string instead of char* , ie const std::string & instead of const char* and std::vector or std::array instead of built in arrays!

链接地址: http://www.djcxy.com/p/82814.html

上一篇: 析构函数不删除分配的内存

下一篇: C ++ delete []调用析构函数