Solved

problem in delete in Debug mode

Posted on 2006-11-16
6
187 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

11 Experts available now in Live!

Get 1:1 Help Now