Solved

problem in delete in Debug mode

Posted on 2006-11-16
6
217 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

860 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