Solved

problem in delete in Debug mode

Posted on 2006-11-16
6
208 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 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