Solved

problem in delete in Debug mode

Posted on 2006-11-16
6
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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