0

I was trying to use destructors for a struct. However, the compiler reported errors that Heap block at 006651F8 modified at 00665229 past requested size of 29 gift1.exe has triggered a breakpoint.

    #include "stdafx.h"

    struct Node{
        char *name;
        int age;

        Node(char *n = 0, int a = 0){
            this->name = _strdup(n);
            age = a;
        }

        ~Node(){
            if (this->name != 0)
                delete[] this->name;
        }
    };

    int _tmain(int argc, _TCHAR* argv[])
    {

        Node node1("Andy", 20);
        return 0;
    }

May I know where went wrong?

Does it matter if I use nameinstead of this->name ?

Add-on:

Even if I change it to free(this->name); as suggested by the answers below, the same error appears. Heap block at 00EA68D8 modified at 00EA6909 past requested size of 29 gift1.exe has triggered a breakpoint.

lxjhk
  • 567
  • 2
  • 7
  • 13
  • 1
    _strdup is a C function (AFAIK) so it most likely used malloc, you cannot delete malloced memory (new -> delete, malloc -> free). You have to free it. But really you should be using std::string. – Borgleader Jan 21 '15 at 17:27
  • BTW `name` and `this->name` do the same – anatolyg Jan 21 '15 at 18:55

2 Answers2

3

_strdup uses malloc() internally:

The _strdup function calls malloc to allocate storage space for a copy of strSource and then copies strSource to the allocated space.

but you are using delete[] to deallocate:

Deallocates storage previously allocated by a matching operator new... If the pointer passed to the standard library deallocation function was not obtained from the corresponding standard library allocation function, the behavior is undefined.

To solve, use std::string instead of a char*(or if you really cannot use std::string use free() and prevent copying or implement copying for Node).

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • I guess "prevent copying or implement copying" is a terse reference to [this](http://stackoverflow.com/q/4172722/509868) – anatolyg Jan 21 '15 at 18:53
  • Even if I change it to `free(this->name);`, the same error appears. `Heap block at 00EA68D8 modified at 00EA6909 past requested size of 29 gift1.exe has triggered a breakpoint.` – lxjhk Jan 22 '15 at 14:49
  • @lxjhk, are you certain you have rebuilt? To be sure, perform a clean and the rebuild. – hmjd Jan 22 '15 at 15:42
  • @hmjd Yes, I have cleaned and rebuilt the solution but the problem persists. I am using MSV 2013. – lxjhk Jan 22 '15 at 15:53
  • @lxjhk, I just did that and no problems (with 2010 and 2013). – hmjd Jan 22 '15 at 16:21
  • @lxjhk, can you post your compiler arguments? – hmjd Jan 22 '15 at 16:28
  • @hmjd May I know how to view the compiler arguments? If I change the setting to `X64`, the program will execute without errors but once I switch to `Win32`, the problem appears. – lxjhk Jan 22 '15 at 16:43
  • If you using the IDE it should be displayed in the output window (I built both x64 and x86 and both worked correctly). – hmjd Jan 22 '15 at 16:53
0

If you check the documentation for _strdup (aka strdup) you will see that it uses malloc internally to allocate the string, therefore the memory must be freed with a call to free instead of delete.

sjdowling
  • 2,994
  • 2
  • 21
  • 31