mfc >> new without delete: what will happen

by bbg » Mon, 27 Jun 2005 12:15:25 GMT

Hi guys, I guess it is 'not good' if memories allocated by new operator are
not deleted by delete operator. Are there any discussions on how it is not
good? bbg




mfc >> new without delete: what will happen

by thatsalok » Mon, 27 Jun 2005 12:42:02 GMT


Memory Leak!!!







mfc >> new without delete: what will happen

by Jonathan Wood » Mon, 27 Jun 2005 14:04:37 GMT

This is referred to as a memory leak as memory that is not released does not
become available for reuse.

In actuality, Windows will now reclaim all memory used by an application
when that application terminates. However, if the program continues to run,
it may continue to use memory that is not released for reuse, and eventually
run out of memory.

Bottom line: It's horribly bad form. When you're done using some memory,
release it so that it may be used again. It's not hard, so there's not much
value in discussing how far we can get without doing so.

--
Jonathan Wood
SoftCircuits
http://www.softcircuits.com
Available for consulting: http://www.softcircuits.com/jwood/resume.htm



are




new without delete: what will happen

by Joseph M. Newcomer » Mon, 27 Jun 2005 15:44:01 GMT

'now reclaim"? Every version of Win32 in existence had this characteristic, and Win16
would do it in most (but not all) cases. But there was never a time in the history of
Win32 when it did not reclaim all the memory on process termination.

But, as observed, failing to free memory is generally considered to be a bug.
joe



Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


new without delete: what will happen

by Jonathan Wood » Mon, 27 Jun 2005 23:23:31 GMT

Joseph,

characteristic, and Win16
history of

I'm sorry. Did you think I said Win32? You are simply mistaken about that.

--
Jonathan Wood
SoftCircuits
http://www.softcircuits.com
Available for consulting: http://www.softcircuits.com/jwood/resume.htm




new without delete: what will happen

by Joseph M. Newcomer » Tue, 28 Jun 2005 00:55:04 GMT

But you said "now reclaim" as if it hasn't been doing it since it was first delivered
1991, where "now" implies an immediacy (e.g., Windows XP does it but Win2K didn't). I
guess I don't consider 14-year-old technology compatible with "now" used to contrast to
some earlier (and now virtually nonexistent) technology.
joe




Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


new without delete: what will happen

by Jonathan Wood » Tue, 28 Jun 2005 01:36:27 GMT

Joseph,

first delivered

Sheesh. Whatever. I implied nothing about a time frame. Perhaps you misread
and thought your opinion needed to be stated.

Unless you think that Windows does not now reclaim memory of applications
when they terminate, then just get over it.

--
Jonathan Wood
SoftCircuits
http://www.softcircuits.com
Available for consulting: http://www.softcircuits.com/jwood/resume.htm




new without delete: what will happen

by bbg » Tue, 28 Jun 2005 07:37:47 GMT

I am curious.. can debugger usually detect memory leak and give warning that
new allocated memory didn't freed?

bbg



not
run,
eventually
much


not




new without delete: what will happen

by Jonathan Wood » Tue, 28 Jun 2005 07:42:00 GMT

No, but the run-time library can. The debug version of MFC will report
memory leaks when the application terminates.

Can you please say why you are so curious about this and why you don't
simply free memory when you no longer need it?

--
Jonathan Wood
SoftCircuits
http://www.softcircuits.com
Available for consulting: http://www.softcircuits.com/jwood/resume.htm



that




operator




new without delete: what will happen

by bbg » Tue, 28 Jun 2005 09:27:25 GMT

I am maintaining program written by some other guy who often forgot delete
after use..bbg







does
application
memory,


is




new without delete: what will happen

by darsant@gmail.com » Tue, 28 Jun 2005 22:19:18 GMT




Start a search of the source code. Find all instances of new. Starting
with the first, find the instance of where it is deleted. If you can't
find it, add it where it should logically be. If you continue through
the new search this way, you should be able to kill 99% of the leaks.

First thing I always do upon using new is adding a delete line, be it
the next line or at the end of the function, just so I don't forget. =)

Josh McFarlane



new without delete: what will happen

by Tom Serface » Tue, 28 Jun 2005 23:35:00 GMT

You can also get BoundsChecker to do a really good job of checking for
leaks. Some leaks are really bad (if they are for memory continually
allocated) some are not so bad (if they are for memory only allocated once
during the life of the program), but all leaks can be overcome.

Tom







new without delete: what will happen

by Johan Rosengren » Wed, 29 Jun 2005 01:15:41 GMT

<snip>


This is very, very good advice. I second it as *the* way of getting rid of
leaks. The same method also work for resource it - write the release just
after the allocation. I still have the occasional leak, but I think they are
few and far between nowadays.

Johan Rosengren
Abstrakt Mekanik AB






new without delete: what will happen

by Doug Harrison [MVP] » Wed, 29 Jun 2005 01:46:20 GMT





Hmmm, I use new regularly, but I hardly ever write delete, and yet I can't
remember the last time I leaked memory. How can this be? I use smart
pointers and classes such as std::vector extensively. Besides making it
much harder to leak memory, this discipline makes it much easier to write
exception-safe code that isn't littered with otherwise redundant try/catch
blocks, that really are nothing more than bad old return code testing in
disguise.

Here are a couple of rules of thumb:

1. If you're writing try/catch a lot, you're probably not using exceptions
and the RAII technique effectively.

2. If you're writing delete a lot, you're probably not using smart pointers
and other classes such as std::vector effectively.

3. If your code is exception-safe, and (2) applies, (1) almost certainly
applies, too.

--
Doug Harrison
Microsoft MVP - Visual C++


new without delete: what will happen

by Doug Harrison [MVP] » Wed, 29 Jun 2005 05:05:27 GMT





To clarify, I use new mainly for scalars that I store in smart pointers,
while std::vector takes the place of array new.

--
Doug Harrison
Microsoft MVP - Visual C++


Similar Threads

1. What happen if delete a CWnd without calling CWnd::DestroyWindow()

After performing the following steps, what happen to my application or the 
system:
1. My application has a class, namely CMyWnd, which is inherited from CWnd.
2. Instantiate a CMyWnd object by: CMyWnd* pMyWnd = new CMyWnd(...).
3. Call pMyWnd->CreateEx(...) to create the window.
4. (**DO NOT perform this step** Call pMyWnd->DestroyWindow() to destroy the 
window.)
5. Release the CMyWnd object by: delete pMyWnd;

Please be note the destructor of CMyWnd does not called DestroyWindow() . 
Here is declaration of the CMyWnd 
class CMyWnd : public CWnd
{ 
  CMyWnd()
  {
  }

  ~CMyWnd()
  {
    // do nothing
  }
}

2. What happens if I use delete [] to a pointer from a "new char('\0')"

3. Mixing new/delete and operator new/delete?

Is it allowed by the C++ standard to mix the use of 'new/delete' and 
'operator new/delete'?

(a) alloc (version 1) + free (version 2)
(b) alloc (version 2) + free (version 1)


// VERSION 1 ALLOC (using operator new + placement new)
type* data = (type*)operator new(n * sizeof(type));
for (type* p = data; p != data + n; ++p)
    new ((void *)p) value_type();

// VERSION 1 FREE (using calling destructor + operator delete)
for (type* p = data; p != data + n; ++p)
    p->~type();
operator delete(data);

// VERSION 2 ALLOC (using new)
type* data = new type[n];

// VERSION 2 FREE (using delete)
delete [] data;

4. Delete Objects Even Without "New"?

5. new without delete

The following code for a singleton implementation is recommended in a
website:

class GlobalClass

{

    int m_value;

    static GlobalClass *s_instance;

    GlobalClass(int v = 0)

    {

        m_value = v;

    }

  public:

    int get_value()

    {

        return m_value;

    }

    void set_value(int v)

    {

        m_value = v;

    }

    static GlobalClass *instance()

    {

        if (!s_instance)

          s_instance = new GlobalClass;

        return s_instance;

    }

};



// Allocating and initializing GlobalClass's

// static data member.  The pointer is being

// allocated - not the object inself.

GlobalClass *GlobalClass::s_instance = 0;



void foo(void)

{

  GlobalClass::instance()->set_value(1);

  cout << "foo: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

}



void bar(void)

{

  GlobalClass::instance()->set_value(2);

  cout << "bar: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

}



int main(void)

{

  cout << "main: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

  foo();

  bar();

}


Is the author at fault for the lack of delete statements?  Are there
memory-leak issues?

Thanks a lot,

Paul Epstein

6. When this statement without &, what will happen?

7. after create a new file use filemapping ,i could not delete the new file

hi,all
in my c programme ,i created a new file used the following
function(a,b,c,d,e,f,g),
after writed bytes to the file,i use CloseHandle to close the file and
MapFile,
CloseHandle return success,but i could not delete the new file until exit
the programme.
Anyone knows it?
thanks
Yongge

a)hFile = CreateFile(lpFileName,
      GENERIC_READ|GENERIC_WRITE,
      0,
      NULL,
      CREATE_NEW,
      FILE_ATTRIBUTE_NORMAL,
      NULL);

b)DWORD dBytesWritten=1024;          // number of bytes written
   char szCharBuff[1024];
   memset((void *)szCharBuff,0,1024);
   WriteFile (hFile, (void *)szCharBuff, 1024,&dBytesWritten, NULL);

c)hMapFile = CreateFileMapping(hFile, // current file handle
         NULL, // default security
         PAGE_READWRITE, // read/write permission
         0,  // size of mapping object, high
         0, // size of mapping object, low
         NULL); // name of mapping object
 d)lpMapAddress = MapViewOfFile(hMapFile, // handle to mapping object
           FILE_MAP_ALL_ACCESS, // read/write permission
           0, // high-order 32 bits of file offset
           dwFileMapStart, // low-order 32 bits of file offset
           dwMapViewSize); // number of bytes to map
 e)UnmapViewOfFile();
 f)b=CloseHandle(hMapFile); // close the file mapping
 g)b=CloseHandle(hFile);  // close the file


8. delete() and new() on elements of an array created by new[]()