Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6123911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:04:20+00:00 2026-05-23T16:04:20+00:00

I am having an error in my C++ application that I am having trouble

  • 0

I am having an error in my C++ application that I am having trouble debugging. I have looked online, and I appear to be doing all of my allocation/deallocation the correct way. Here is my code:

template <typename T>
class Matrix
{
private:
   int _rows;
   int _cols;
   T** _matrix;
public:
   Matrix(int r, int c);
   ~Matrix();
   T GetValue(int r, int c);
};

template <typename T>
Matrix<T>::Matrix(int r, int c)
{
   _rows = r;
   _cols = c;

   _matrix = new T*[_rows];
   for(int i = 0; i < _rows; i++)
      _matrix[i] = new T[_cols];

   for(int i = 0; i < _rows; i++)
      for(int j = 0; j < _cols; j++)
         _matrix[i][j] = NULL;
}

template <typename T>
Matrix<T>::~Matrix()
{
   for(int i = 0; i < _rows; i++)
      delete [] _matrix[i];
   delete [] _matrix;
}

template <typename T>
T Matrix<T>::GetValue(int r, int c)
{
   if(r < 0 || r >= _rows || c < 0 || c > _cols)
   {
      throw -1;
      return NULL;
   }

   return _matrix[r][c];
}

And my client code…

int main()
{
   Matrix<int> myMatrix(3, 3);
   myMatrix.GetValue(1, 1);
   // myMatrix.~Matrix();  // Don't do this anymore
}

As soon as the variable “myMatrix” goes out of scope, I get this error:

Unhandled exception at 0x103159da (msvcr1000d.dll)…Access violation reading location 0xfeeefee2.
And I am brought to the file “dbgdel.cpp”
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

Please help!


EDIT:

Okay, I neglected to give some information. Please see below:

I have an additional method called “T Dot(Matrix)” I’ve also got two methods called “Columns()” and “Rows()” which are just getters for _cols and _rows. And a method called “SetValue(int r, int c, T value)” which sets _matrix[r][c] = value. I don’t think showing the implementation is necessary for these.

template <typename T>
T Matrix<T>::Dot(Matrix<T> m)
{
   if(_cols > 1 || m.Columns() > 1 || _rows != m.Rows())
   {
      throw -1;
      return NULL;
   }

   T value = 0;
   for(int i = 0; i < _rows; i++)
   {
      value += _matrix[i][0] * m.GetValue(i, 0);
   }
   return value; // Whoops, this was here, just forgot to type it
}

And the client…

int main()
{
   Matrix<int> intM1(3, 1);
   Matrix<int> intM2(3, 1);

   intM1.SetValue(0, 0, 1);
   intM1.SetValue(1, 0, 1);
   intM1.SetValue(2, 0, 1);
   intM2.SetValue(0, 0, 1);
   intM2.SetValue(1, 0, 1);
   intM2.SetValue(2, 0, 1);

   std::cout << intM1.Dot(intM2) << endl;
}

This generates the same error as above, but only when the “Dot()” function is called.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:04:20+00:00Added an answer on May 23, 2026 at 4:04 pm

    With your edit, the issue here is still the lack of a copy constructor and assignment operator (see my answer to your last question). The problem is that when you pass the Matrix by value into a function, the copy constructor is invoked to make the copy, but since you haven’t defined one C++ will use the default copy constructor, which just makes a shallow copy. Consequently, you’ll end up getting a new Matrix that shares a pointer to the same elements as the old Matrix. When this new Matrix goes out of scope, its destructor will fire, cleaning up the array used by the other Matrix. When the original Matrix then goes out of scope and is cleaned up, it will try to delete an already-deleted array, causing the crash.

    To fix this, you need to implement a copy constructor and assignment operator that correctly duplicate the resources. There’s a rule-of-thumb called the Rule of Three that says that if you have a destructor, you also need a copy constructor and assignment operator to prevent these sorts of bugs. Try implementing these missing functions and see if the problem clears up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble with modifying a php application to have pagination. My error
I have a facebook iframe application that is having trouble redirecting, I keep getting
I'm having some trouble with my error handling in my ASP.NET MVC 2 application.
Been having trouble with my application that finds a users location and displays it
I'm having some trouble with an application that we're porting to Mono. (For reference,
I am having this error in my j2ee web application. java.sql.SQLException: ORA-00604: error occurred
I keep having an error when running my web application. The error does not
My users are having an intermittent error when using a Windows Forms application built
I'm having a really frustrating error trying to secure an ASP.NET application using the
I have searched and searched and all I could see was that to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.