Considerations for delete class object pointers]

Source: Internet
Author: User

http://blog.csdn.net/infoworld/article/details/45560219

Scene:
1. C + + classes have constructors and destructors that are used to release resources when class objects are deleted (or when local variables are automatically destroyed).

2. C + + Class object Pointers in many cases need to be assigned to void* universal pointers to achieve the purpose of transmitting objects, but often this void* pointer is the source of memory leaks or program errors,

This is why C + + has a generic purpose, and it is also designed to eliminate this object uncertainty at compile time, avoiding delete or use errors.

3. When you delete void* type, be careful to convert to class type only delete, such as delete (A *) Data_;

OK, look at the code, what's wrong with the following code?

[CPP]View Plaincopy
  1. Test_class.cpp: Defines the entry point of the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. Class A
  6. {
  7. Public
  8. A ()
  9. {
  10. i = new int;
  11. }
  12. ~a ()
  13. {
  14. Delete I;
  15. }
  16. int* i;
  17. };
  18. Class B
  19. {
  20. Public
  21. B (void* data)
  22. {
  23. Data_ = data;
  24. }
  25. ~b ()
  26. {
  27. Delete Data_;
  28. }
  29. void* Data_;
  30. };
  31. Template <class t>
  32. Class C
  33. {
  34. Public
  35. C (t* data)
  36. {
  37. Data_ = data;
  38. }
  39. ~c ()
  40. {
  41. Delete Data_;
  42. }
  43. t* Data_;
  44. };
  45. void Wrong ()
  46. {
  47. A *a = new A ();
  48. b b (a); when the function returns, A's destructor does not call
  49. }
  50. void Right ()
  51. {
  52. A *a = new A ();
  53. C<a> c (A); when the function returns, A's destructor is called
  54. }
  55. int _tmain (int argc, _tchar* argv[])
  56. {
  57. Wrong ();
  58. Right ();
  59. return 0;
  60. }

Analytical:

B in the destructor Deleta Data_, looking at the disassembly code, and did not call the destructor.

[Plain]View Plaincopy
    1. 011d1643 mov Eax,dword ptr [this]
    2. 011d1646 mov ecx,dword ptr [eax]
    3. 011d1648 mov dword ptr [EBP-0D4H],ECX
    4. 011D164E mov edx,dword ptr [ebp-0d4h]
    5. 011d1654 push edx
    6. 011d1655 call operator Delete (11d1096h)


C in the destructor Deleta Data_, look at the disassembly code, there are call destructors.

[Plain]View Plaincopy
    1. 011d1883 mov Eax,dword ptr [this]
    2. 011d1886 mov ecx,dword ptr [eax]
    3. 011d1888 mov dword ptr [EBP-0D4H],ECX
    4. 011d188e mov edx,dword ptr [ebp-0d4h]
    5. 011d1894 mov dword ptr [Ebp-0e0h],edx
    6. 011D189A cmp DWORD ptr [ebp-0e0h],0
    7. 011d18a1 JE c<a>::~c<a>+58h (11d18b8h)
    8. 011D18A3 Push 1
    9. 011D18A5 mov ecx,dword ptr [ebp-0e0h]
    10. 011d18ab call A:: ' scalar deleting destructor ' (11D102DH)
    11. 011D18B0 mov dword ptr [Ebp-0e8h],eax
    12. 011d18b6 jmp c<a>::~c<a>+62h (11D18C2H)

Considerations for delete class object pointers]

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.