0

So I am trying to delete my member pointer m_memory as it points to an array of char. When I try to delete the array that m_memory points to using delete[] I end up triggering breakpoints. I tried initializing m_memory as a nullptr before using new but it still triggered the breakpoint. Just wondering what my error is.

Header File:

#ifndef FISH_H
#define FISH_H
#include <iostream>
class Fish {
public:
    Fish(int capacity, std::string name);// Constructor
    Fish(const Fish &other); // Copy Constructor
    ~Fish(); // Destructor
    Fish &operator=(const Fish &other); // Assignment Operator
    void remember(char c); // Remember c
    void forget(); // Clears memory by filling in '.'
    void printMemory() const;// Prints memory
    std::string getName();
protected:
    const char* getMemory() const;// Returns memory
    int getAmount() const; // Returns amount remembered
    int getCapacity() const; // Returns memory capacity
private:
    std::string m_name;
    char * m_memory;
    int m_capacity;
    int m_remembered;
int m_replace;

};
#endif

Implementation file:

#include “fish.”

Fish::Fish(int capacity, std::string name) {// Constructor
    this->m_capacity = capacity;
    if (capacity > 0) {
        this->m_capacity = capacity;
    }
    else {
        this->m_capacity = 3;
    }

this->m_name = name;
this->m_memory = new char[this->m_capacity];
this->m_memory[this->m_capacity] = '\0';

for (int i = 0; i < this->m_capacity; i++) {
    this->m_memory[i] = '.';
    }
    this->m_remembered = 0;
    this->m_replace = 0;
}


Fish::~Fish() // Destructor
    {
        delete [] this->m_memory; //exception thrown, breakpoint triggered
    }

Main:

#include "fish.h"
int main() {
    Fish *nemo = new Fish(3, "nemo");
    nemo->~Fish();

}
16
  • I also wanted to mention that I tried deleting the pointer without prepending this-> to no avail. Commented Mar 26, 2018 at 21:50
  • 2
    Vaguely related: You rarely want to call the destructor yourself, so nemo->~Fish(); should be delete nemo; Commented Mar 26, 2018 at 21:51
  • Can we assume that there is a restriction in place preventing use of std::string? Commented Mar 26, 2018 at 21:52
  • 1
    No. You won't have to call the destructor. Make the destructor virtual and through the magic of polymorphism the correct destructors will be called regardless of whether operating on base classes or subclasses. Commented Mar 26, 2018 at 21:55
  • 2
    Show the code for the copy constructor. That's very likely to be where the problem is. Commented Mar 26, 2018 at 22:03

1 Answer 1

4
this->m_memory = new char[this->m_capacity];
this->m_memory[this->m_capacity] = '\0';

This writes outside the array. this->m_capacity is 3, so you're allocating 3 chars and then writing to the 4th one.

The computer isn't required to detect this problem. Microsoft is trying to be helpful here by detecting it, but their detection only happens when you free the memory. If you run your program in the debugger, you should see some messages in the Debug Output window explaining why your program crashed.

Sign up to request clarification or add additional context in comments.

22 Comments

So I guess the solution would be to make m_capacity = capacity + 1 so I can explicitly add the "/0" terminating character. I like to add the null termination character myself because I've had problems in the past of my arrays having more than what I allocated in it and that seemed to solve the problem.
Would that solution prevent the breakpoint and or is adding the null terminating character causing the breakpoint and preventing me from using delete [] properly?
@Hikikomori Your second comment tells me I don't think you understood the problem.
Putting a null terminator in an array doesn't solve the problem of trying to put more in the array than is allocated. That means you have other bugs elsewhere in your code. You likely aren't taking the array's capacity into account when filling the array, or when copying/printing the array.
The bad code is in the constructor, removing the null terminating character ended up letting me delete [] m_memory but ended up having appended garbage to the m_memory array when i looked at it in the local window on debug mode.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.