Access violation reading location 0xCCCCCCBC in destructor

-1

I'm having an issue when debugging my program. I am getting the following error when the destructor is called :0xC0000005: Access violation reading location 0xCCCCCCBC. I assume it is some sort of memory leak issue but I just can't find the culprit. Here is the destructor and the functions that modify the dynamic array:

Contact::~Contact() {

        delete[] phoneNums;
        phoneNums = nullptr;
    }

    Contact::Contact(const Contact& src) {
        phoneNums = nullptr;
        *this = src;
    }

    Contact& Contact::operator=(const Contact& src) {
        if (this != &src) {
            numPhoneNums = src.numPhoneNums;
            strncpy(name, src.name, MAX_CHAR);
            delete[] phoneNums;

            if (src.phoneNums != nullptr) {
                phoneNums = new long long[numPhoneNums];
                for (int i = 0; i < numPhoneNums; i++) {

                    phoneNums[i] = src.phoneNums[i];
                }
            }
            else {
                phoneNums = nullptr;
            }
        }
        return *this;
    }

    Contact& Contact::operator+=(long long newNum) {
        if (getValidPhone(newNum) && !isEmpty()) {
            numPhoneNums++;
            long long* temp = nullptr;
            temp = new long long[numPhoneNums];
            for (int i = 0; i < numPhoneNums - 1; ++i) {
                temp[i] = phoneNums[i];
            }
            temp[numPhoneNums - 1] = newNum;

            delete[] phoneNums;
            phoneNums = nullptr;
            phoneNums = temp;
            }
        return *this;
    }
Contact::Contact() {
    name[0] = '\0';
    phoneNums = nullptr;
    numPhoneNums = 0;
}

int main() {
    cout << "----------------------------------------" << endl;
    cout << "Maximum no of characters in a name: " << sict::MAX_CHAR << endl;
    sict::Contact theContact("John Doe", nullptr, 0); // sict:: intentional
    theContact.display();
    theContact += 14161234567LL;
    theContact += 14162345678LL;
    theContact += 14163456789LL;
    theContact += 114164567890LL;
    theContact.display();

    cout << endl << "Testing! Please wait:" << endl;

    for (int i = 1; i <= 5000000; i++)
    {
        Contact temp = theContact;
        theContact = temp;
        theContact = theContact;
        if (!(i % 10000))
            cout << ".";
        if (!(i % 500000))
            cout << endl;
    }
    cout << endl;
    theContact.display();

    theContact = Contact("", nullptr, 0);
    theContact += 14161230002LL;//program crashes here
    theContact.display();
    system("pause");
    return 0;
}
c++
asked on Stack Overflow Nov 2, 2018 by RobbieD • edited Nov 2, 2018 by RobbieD

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0