PRB: Memory Leak Using Delete Operator

Last reviewed: July 22, 1997
Article ID: Q118679
7.00 | 1.00 1.50 MS DOS | WINDOWS kbprg kbprb

The information in this article applies to:

  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5

SYMPTOMS

When you release dynamically allocated memory back to the system by using the C++ delete operator in a QuickWin application, a memory leak occurs.

CAUSE

The run-time library provides four versions of the delete operator to deallocate pointers that are near, far, huge, or based. The version that is selected depends on the addressing mode of pointer. For example, if the pointer is a near pointer, the near delete operator is called. If the addressing mode of pointer does not reflect the version of the new operator used to allocate the memory, the incorrect version of the delete operator is called. For example:

   Node __far *fpN;
   fpN = new Node __near;  // convert near to far
   delete fpn;             // far delete invoked for near object

Here, the compiler chooses the inappropriate delete operator for the pointer, which results in a run-time error.

RESOLUTION

To prevent this problem, explicitly cast the pointer to the addressing mode you want:

   delete (Node __near *)fpN;

MORE INFORMATION

The following sample code can be used to demonstrate the memory leak. Replace the last two uncommented lines with the commented lines to see the correct output.

Sample Code

   /* Compile options needed: /Mq
   */

   #include <malloc.h>
   #include <stdio.h>

   void main()
   {
      char * nearptr;
      char far * farptr;

      printf("\n");
      printf("Starting memory: %u\n",_memavl());

      nearptr = new char;
      farptr = new char __near; // new returns a near pointer
      printf("After memory allocated: memavl=%u\n",_memavl());

      delete nearptr; // the near delete gets called

      delete farptr;  // the far delete gets called for near memory
      printf("After memory is freed : memavl=%u\n",_memavl());

      //delete ( char __near *) farptr; // this will call the correct
   delete
      //printf("After memory is freed : memavl=%u\n",_memavl());
   }


Additional reference words: 7.00 1.00 1.50
KBCategory: kbprg kbprb
KBSubcategory: CPPLngIns
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.