title: Getting CrtIsValidHeapPointer Error when trying to release memory [C++]
body: <p>I have an exercise and I need to create an Engine class and a Car class.</p>
  
  <p>Those are parts of my code:</p>
  
  <p><strong>Car.cpp</strong></p>
  
  <pre><code>Car::Car()
  {
      _engine = new Engine();
      _manufacturer = new char[10];
      _prod_year = 0;
      _color = new char[10];
  }
  
  Car::~Car()
  { 
      // Releasing allocated memory
      delete[] _manufacturer;
      delete[] _color;
      delete _engine;
  }
  
  void Car::print(void) const
  {
      (*_engine).print();
      cout &lt;&lt; "\n" &lt;&lt; endl;
      cout &lt;&lt; "Manufacturer:" &lt;&lt; _manufacturer &lt;&lt; "\n" &lt;&lt; endl;
      cout &lt;&lt; "Production Year:" &lt;&lt; _prod_year &lt;&lt; "\n" &lt;&lt; endl;
      cout &lt;&lt; "Color:" &lt;&lt; _color &lt;&lt; endl;
  }
  </code></pre>
  
  <p><strong>Car.h</strong></p>
  
  <pre><code>class Car
  {
  
      Engine * _engine;
      char * _manufacturer;
      int _prod_year;
      char * _color;
  
      public:
  
      /*
      * Creates a Car with the default set of attributes.
      */
  
      Car();
      virtual ~Car();
      inline Engine * GetEngine() { return _engine; }
      void SetEngine(Engine * engine) { _engine = engine; }
      inline char * GetManufacturer(){ return _manufacturer; }
      void SetManufacturer(char * manufacturer){_manufacturer = manufacturer;}
      inline int GetProdYear() { return _prod_year; }
      void SetProdYear(int prod_year) { _prod_year = prod_year; }
      inline char * GetColor() { return _color; }
      void SetColor(char * color) { _color = color; }
      void print(void) const;
  };
  </code></pre>
  
  <p><strong>Engine.h</strong></p>
  
  <pre><code>class Engine
  {
      /*  
          Represents an Engine with the following attributes:
          * int Hourse Power
          * char * Manufacturer
          * int Production Year
      */
  
      int _horse_power;
      char * _manufacturer;
      int _prod_year;
  
  public:
      Engine();
      virtual ~Engine();
      int GetHorsePower() { return _horse_power; }
      void SetHorsePower(int horse_power) { _horse_power = horse_power; }
      char * GetManufacturer(){ return _manufacturer; }
      void SetManufacturer(char * manufacturer){_manufacturer = manufacturer;}
      int GetProdYear() { return _prod_year; }
      void SetProdYear(int prod_year) { _prod_year = prod_year; }
      void print() const;
  
  };
  </code></pre>
  
  <p><strong>Exc.cpp</strong></p>
  
  <pre><code>Engine engine;
  engine.SetHorsePower(150);
  cout &lt;&lt; "Current Horse Power: " &lt;&lt;engine.GetHorsePower()&lt;&lt;endl;
  
  char * manufacturer = new char[strlen("VP") +1];
  strcpy(manufacturer, "VP");
  engine.SetManufacturer(manufacturer);
  cout &lt;&lt; "Current Manufacturer: " &lt;&lt; engine.GetManufacturer() &lt;&lt; endl;
  
  engine.SetProdYear(1995);
  cout &lt;&lt; "Current Production Year: " &lt;&lt; engine.GetProdYear() &lt;&lt; endl;
  
  cout &lt;&lt; "\n ------------- Printing engine details -------------\n" &lt;&lt; endl;
  engine.print();
  Car car;
  car.SetEngine(&amp;engine);
  cout &lt;&lt; "Current Engine: " &lt;&lt; endl;
  (*car.GetEngine()).print();
  
  char * car_manufacturer = new char[strlen("Toyota") + 1];
  car_manufacturer = { "Toyota" };
  car.SetManufacturer(car_manufacturer);
  cout &lt;&lt; "Current Car Manufacturer: " &lt;&lt; car.GetManufacturer() &lt;&lt; endl;
  
  car.SetProdYear(2010);
  cout &lt;&lt; "Current Car Production Year: " &lt;&lt; car.GetProdYear() &lt;&lt; endl;
  
  char * color = new char[strlen("Yellow") + 1];
  color = { "Yellow" };
  car.SetColor(color);
  cout &lt;&lt; "Current Car Color: " &lt;&lt; car.GetColor() &lt;&lt; endl;
  
  cout &lt;&lt; "\n ------------- Printing car details -------------\n" &lt;&lt; endl;
  
  car.print();
  </code></pre>
  
  <p>My program is doing ok until it gets to ~Car and then I get "CrtIsValidHeapPointer".</p>
  
  <p>Can someone tell me what I did wrong?</p>
  
  <p>Thx.</p>
  
  <hr>
  
  <h2><strong>SOLUTION</strong></h2>
  
  <pre><code>Engine * engine = new Engine;
  engine-&gt;SetHorsePower(150);
  cout &lt;&lt; "Current Horse Power: " &lt;&lt; engine-&gt;GetHorsePower()&lt;&lt;endl;
  
  char * manufacturer = new char[strlen("VP") +1];
  strcpy(manufacturer, "VP");
  engine-&gt;SetManufacturer(manufacturer);
  cout &lt;&lt; "Current Manufacturer: " &lt;&lt; engine-&gt;GetManufacturer() &lt;&lt; endl;
  
  engine-&gt;SetProdYear(1995);
  cout &lt;&lt; "Current Production Year: " &lt;&lt; engine-&gt;GetProdYear() &lt;&lt; endl;
  
  cout &lt;&lt; "\n ------------- Printing engine details -------------\n" &lt;&lt; endl;
  engine-&gt;print();
  
  Car car;
  car.SetEngine(engine);
  cout &lt;&lt; "Current Engine: " &lt;&lt; endl;
  car.GetEngine()-&gt;print();
  
  char * car_manufacturer = new char[strlen("Toyota") + 1];
  strcpy(car_manufacturer, "Toyota");
  car.SetManufacturer(car_manufacturer);
  cout &lt;&lt; "Current Car Manufacturer: " &lt;&lt; car.GetManufacturer() &lt;&lt; endl;
  
  car.SetProdYear(2010);
  cout &lt;&lt; "Current Car Production Year: " &lt;&lt; car.GetProdYear() &lt;&lt; endl;
  
  char * color = new char[strlen("Yellow") + 1];
  strcpy(color, "Yellow");
  car.SetColor(color);
  cout &lt;&lt; "Current Car Color: " &lt;&lt; car.GetColor() &lt;&lt; endl;
  
  cout &lt;&lt; "\n ------------- Printing car details -------------\n" &lt;&lt; endl;
  
  car.print();
  
  return 0;
  </code></pre>