Usage of cmemorystate

Source: Internet
Author: User

Specifically, memory management in C ++ is quite complex. When applying for memory, you must remember to release the memory. New matches Delete, new [] matches Delete []. the more difficult thing is that it involves modules, which will not be discussed for the time being. I will share it with you later. Let's talk about the cmemorystate first. If you have any shortcomings or errors, please kindly advise. Thank you.

In MFC, cmemorystate can be used to detect memory leaks. memory leaks are caused by incorrect use of the C ++ new or delete operator in memory allocation and retrieval. We can create a cmemorystate object to quickly view the current memory allocation. Let's just move on to the code!

1. No Delete.

1. cmemorystate olf8, newmem, difmem;

2. olmoodle. Checkpoint (); // checks the current memory usage

3.Char* C = new
Char[6];

4. trace0 ("1 -----------------/N ");

5. olmoodle. dumpallobjectssince (); // oldmem will be detected here

6. trace0 ("2 -------------------/N ");

7. newmem. Checkpoint (); // No Delete [] C

8. If (difmem. Difference (olf8, newmem) // compare

9 .{

10. trace0 ("memory lack! /N ");

11 .}

12. trace0 ("3 -------------------/N ");

13. difmem. dumpstatistics (); // print the result in the output.

  1. Trace0 ("4 -----------------/N ");

Output:

1 -------------------
Dumping objects->
E:/test/CCing. cpp (101): {140} normal block at 0x003b8a10,6 bytes long.

Data: <> CD
Object dump complete.
2 -------------------
Memory lack!
3 -------------------

0 bytes in 0 free blocks.
6 bytes in 1 normal blocks.
0 bytes in 0 CRT blocks.
0 bytes in 0 ignore blocks.
0 bytes in 0 client blocks.
Largest number used: 0 bytes.
Total allocations: 6 bytes.
4 -------------------
// Note the following content for Compiler output

Detected memory leaks!
Dumping objects->
E:/test/CCing. cpp (101): {140} normal block at 0x003b8a10, 6 bytes long.
Data: <> CD
Object dump complete.

We can see that there is a description of how much memory is applied, where the memory is allocated, and a prompt for Memory leakage!

2. Add Delete

1. Delete [] C; // others remain unchanged

2. newmem. Checkpoint (); // Add Delete [] C

Output:

2 -------------------
3 -------------------

0 bytes in 0 free blocks.
0 bytes in 0 normal blocks.
0 bytes in 0 CRT blocks.
0 bytes in 0 ignore blocks.
0 bytes in 0 client blocks.
Largest number used: 0 bytes.
Total allocations: 6 bytes.
4 -------------------

Are released.

3. Test some data types.

(1). built-in data types

1. olf8. Checkpoint (); // checks the current memory usage

2.IntI = 1; // others remain unchanged

3.DoubleB = 2;

4.CharA = '1 ';

  1. CharCh [6];

Output:

2 -------------------
3 -------------------
0 bytes in 0 free Blocks
0 bytes in 0 normal blocks.
0 bytes in 0 CRT blocks.
0 bytes in 0 ignore blocks.
0 bytes in 0 client blocks.
Largest number used: 0 bytes.
Total allocations: 0 bytes.

Built-inThe type is not constructed and cannot be checked against C's malloc/free. globalalloc/globalfree or localalloc/localfree!

(2). c ++ type string

1. olf8. Checkpoint (); // checks the current memory usage

  1. String S = "123456 ";

Output:

2 -------------------
3 -------------------
0 bytes in 0 free Blocks
0 bytes in 0 normal blocks.
0 bytes in 0 CRT blocks.
0 bytes in 0 ignore blocks.
0 bytes in 0 client blocks.
Largest number used: 0 bytes.
Total allocations: 0 bytes.

This is STL. STL has a set of memory management methods.

(3). For the MFC type cstring

1. olf8. Checkpoint (); // checks the current memory usage

  1. Cstring STR = _ T ("123456 ");

Output:

1 -------------------
Dumping objects->
F:/SP/vctools/vc7libs/ship/atlmfc/src/mfc/strcore. cpp (141):{ 147} normal block at 0x003b8cc0,
23 bytes long.
Data: <,? X> 2C 08 3f 78 06 00 00 00 00 00 00 01 00 00 00
Object dump complete.

2 -------------------
Memory lack!
3 -------------------

0 bytes in 0 free blocks.
23 bytes in 1 normal blocks.
0 bytes in 0 CRT blocks.
0 bytes in 0 ignore blocks.
0 bytes in 0 client blocks.
Largest number used: 0 bytes.
Total allocations: 23 bytes.
4 -------------------

// No detected memory leaks!Because the cstring object is released when appropriate

We can see that this is the issue of MFC. There are differences between the two memory blocks, but there is no reminderDetected memory leaks!

Conclusion: When we use memory detection, it is best to test new and delete instead of other data types. It can also detect the memory usage after a function is called.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.