Skip to content

Commit f68fd5e

Browse files
author
Colin Robertson
authored
Merge pull request #3336 from MicrosoftDocs/FromPublicMasterBranch
Confirm merge from FromPublicMasterBranch to master to sync with https://github.com/MicrosoftDocs/cpp-docs (branch master)
2 parents 77fb3bb + c75fb8a commit f68fd5e

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

docs/code-quality/c26401.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
---
22
title: C26401
3-
ms.date: 07/21/2017
3+
ms.date: 12/14/2020
44
ms.topic: "conceptual"
55
f1_keywords: ["C26401"]
66
helpviewer_keywords: ["C26401"]
77
ms.assetid: b9d3d398-697a-4a5d-8bfe-9c667dffb90b
8-
description: CppCoreCheck rule that enforces C++ Core Guidelines I.11
8+
description: CppCoreCheck rule C26401 enforces C++ Core Guidelines I.11
99
---
1010
# C26401 DONT_DELETE_NON_OWNER
1111

12-
This check detects places where moving to `owner<T>` can be a good option for the first stage of refactoring. Like C26400 it enforces rules I.11 and R.3, but focuses on the "release" portion of the pointer lifetime. It warns on any call to operator **`delete`** if its target is neither an `owner<T>` nor an implicitly assumed owner. For more information, see [C26400](c26400.md) regarding the **`auto`** declarations. This does include expressions that refer to global variables, formals, and so on.
12+
This check detects places where moving to `owner<T>` can be a good option for the first stage of refactoring. Like C26400, it enforces rules I.11 and R.3, but focuses on the "release" portion of the pointer lifetime. It warns on any call to operator **`delete`** if its target isn't an `owner<T>` or an implicitly assumed owner. For more information about **`auto`** declarations, see [C26400](c26400.md). This check includes expressions that refer to global variables, formal parameters, and so on.
1313

14-
Warnings C26400 and C26401 always occur with [C26409](c26409.md), but they are more appropriate for scenarios where immediate migration to smart pointers is not feasible. In such cases the `owner<T>` concept can be adopted first and C26409 may be temporarily suppressed.
14+
Warnings C26400 and C26401 always occur with [C26409](c26409.md), but they're more appropriate for scenarios where immediate migration to smart pointers isn't feasible. In such cases, the `owner<T>` concept can be adopted first, and C26409 may be temporarily suppressed.
1515

1616
## See also
1717

1818
[C++ Core Guidelines I.11](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#i11-never-transfer-ownership-by-a-raw-pointer-t-or-reference-t)
1919

20-
## Example
20+
## Examples
2121

2222
```cpp
2323
struct myStruct {};
@@ -45,3 +45,25 @@ void function()
4545
delete pMyStruct; // no warning.
4646
}
4747
```
48+
49+
There's a C++ idiom, `delete this`, that triggers this warning. The warning is intentional, because the C++ Core Guidelines discourage this pattern. You can suppress the warning by using the `gsl::suppress` attribute, as shown in this example:
50+
51+
```cpp
52+
class MyReferenceCountingObject final
53+
{
54+
public:
55+
void AddRef();
56+
void Release() noexcept
57+
{
58+
ref_count_--;
59+
if (ref_count_ == 0)
60+
{
61+
[[gsl::suppress(i.11)]]
62+
delete this;
63+
}
64+
}
65+
private:
66+
unsigned int ref_count_{1};
67+
};
68+
```
69+

docs/code-quality/c26409.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
description: "Learn more about: C26409 NO_NEW_DELETE"
2+
description: "Learn more about CppCoreCheck rule C26409: avoid explicit new and delete."
33
title: C26409
4-
ms.date: 08/20/2020
4+
ms.date: 12/14/2020
55
ms.topic: "conceptual"
66
f1_keywords: ["C26409"]
77
helpviewer_keywords: ["C26409"]
@@ -11,7 +11,7 @@ ms.assetid: a3b3a229-d566-4be3-bd28-2876ccc8dc37
1111

1212
> `Avoid calling new and delete explicitly, use std::make_unique<T> instead (r.11).`
1313
14-
Even if code is clean of calls to`malloc()` and `free()`, we still suggest that you consider better options than explicit use of operators [`new` and `delete`](../cpp/new-and-delete-operators.md).
14+
Even if code is clean of calls to `malloc` and `free`, we still suggest that you consider better options than explicit use of operators [`new` and `delete`](../cpp/new-and-delete-operators.md).
1515

1616
**C++ Core Guidelines**:\
1717
[R.11: Avoid calling new and delete explicitly](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r11-avoid-calling-new-and-delete-explicitly)
@@ -22,7 +22,7 @@ The ultimate fix is to use smart pointers and appropriate factory functions, suc
2222

2323
- The checker warns on calls to any kind of operator **`new`** or **`delete`**: scalar, vector, overloaded versions (global and class-specific), and placement versions. The placement **`new`** case may require some clarifications in the Core Guidelines for suggested fixes, and may be omitted in the future.
2424

25-
## Example
25+
## Examples
2626

2727
This example shows C26409 is raised for explicit **`new`** and **`delete`**. Consider using smart pointer factory functions such as `std::make_unique` instead.
2828

@@ -35,3 +35,24 @@ void f(int i)
3535
auto unique = std::make_unique<int[]>(i); // prefer using smart pointers over new and delete
3636
}
3737
```
38+
39+
There's a C++ idiom, `delete this`, that triggers this warning. The warning is intentional, because the C++ Core Guidelines discourage this pattern. You can suppress the warning by using the `gsl::suppress` attribute, as shown in this example:
40+
41+
```cpp
42+
class MyReferenceCountingObject final
43+
{
44+
public:
45+
void AddRef();
46+
void Release() noexcept
47+
{
48+
ref_count_--;
49+
if (ref_count_ == 0)
50+
{
51+
[[gsl::suppress(i.11)]]
52+
delete this;
53+
}
54+
}
55+
private:
56+
unsigned int ref_count_{1};
57+
};
58+
```

docs/mfc/reference/clistctrl-class.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ CSize ApproximateViewRect(
267267
The proposed dimensions of the control, in pixels. If dimensions are not specified, the framework uses the current width or height values of the control.
268268

269269
*iCount*<br/>
270-
Number of items to be displayed in the control. If this parameter is -1, the framework uses the total number of items currently in the control.
270+
Number of items to be displayed in the control. Pass -1 to use the total number of items currently in the control.
271271

272272
### Return Value
273273

@@ -3650,7 +3650,7 @@ BOOL SetItemState(
36503650
### Parameters
36513651

36523652
*nItem*<br/>
3653-
Index of the item whose state is to be set.
3653+
Index of the item whose state is to be set. Pass -1 to apply the state change to all items.
36543654

36553655
*pItem*<br/>
36563656
Address of an [LVITEM](/windows/win32/api/commctrl/ns-commctrl-lvitemw) structure, as described in the Windows SDK. The structure's `stateMask` member specifies which state bits to change, and the structure's `state` member contains the new values for those bits. The other members are ignored.

0 commit comments

Comments
 (0)