Solved

problem in delete in Debug mode

Posted on 2006-11-16
6
197 Views
Last Modified: 2010-04-01
Hi experts,

I have a code, which generates an "invalid memory access" exception when I close my application. That is, I am getting exception when the destructor of my class is called.

In constructor, I am allocating (new) NO_OF_BUFFS amount of memory chunks. And in desctuctor, I am deleting these. I am not able to understand why I am getting exception everytime I delete.

**The exception occurs only in Debug mode, not it Release mode.**

I have also attached the code snippet for your reference:

Class Declaration:
----------------------
typedef unsigned char UINT8;
UINT8*      m_ppCBuffer[NO_OF_BUFFS];

Constructor:
----------------------
for(int i = 0; i < NO_OF_BUFFS;  i++)
{
      m_ppCBuffer[i] = new UINT8[BUFF_SIZE];
      if(!m_ppCBuffer[i])
      {
            MessageBox(NULL, TEXT("Not able to allocate enough memory"), NULL, NULL);
            break;
      }
      m_acBufferStatus[i] = READY_FOR_WRITE;
}

Desctructor:
----------------------
for(int i = 0;
i < NO_OF_BUFFS;
i++)
{
      if(m_ppCBuffer[i])
      {
            delete [] (m_ppCBuffer[i]);
            //delete (m_ppCBuffer[i]);
            m_ppCBuffer[i] = NULL;
      }
}

My doubt is, should I allocate memory for m_ppCBuffer also?

Thanks in advance,
Vikram_B.
0
Comment
Question by:Vikram_B
  • 4
6 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17955259
>> In constructor, I am allocating (new) NO_OF_BUFFS amount of memory chunks. And in desctuctor, I am deleting
>> these. I am not able to understand why I am getting exception everytime I delete.
Somewhere in your code you are you are crossing the allocated size BUFF_SIZE.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17955270
In general, when you deallocate an allocated memory,
and if any crash comes it indicates that you are crossing the boundary of allocated space.
Please check the other part of your code.

I hope it helps.
0
 

Author Comment

by:Vikram_B
ID: 17955304

> Somewhere in your code you are you are crossing the allocated size BUFF_SIZE

But, I am getting that error only when deleting (in the destructor).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17962374
I think you didn't get my point.
Check this code.

class testClass
{
      char* m_str;

public:
      testClass() {
            m_str = new char[1]; // Allocating
      }

      ~testClass() {
            delete [] m_str; // Deallocating. Will crash here.
      }

      void crossBoundary() {
            m_str[1] = 10; // Here I am crossing boundary.
      }
};

int main()
{
      testClass t;
      t.crossBoundary();
      return (0);
}
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 25 total points
ID: 17962385
You can clearly see from the above code.
>> m_str = new char[1]; // Allocating
Allocated 1 char space here.

>> m_str[1] = 10;
Assigning value in the 2nd char which I didn't allocate.

Hope that helps.
0
 
LVL 6

Expert Comment

by:SJT2003A
ID: 17966655
Simply remove the following line in your destructor.
Once after you delete the memory allocation, you can not assign a value though it is a NULL.

  m_ppCBuffer[i] = NULL;  // remove this line completely

Good luck :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now