Skip to content

Commit c9d0043

Browse files
Merge pull request #4837 from MicrosoftDocs/main638373115079393557sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents 26085a2 + 2335f25 commit c9d0043

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed
Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,73 @@
11
---
22
description: "Learn more about: Compiler Warning (level 1) C4251"
33
title: "Compiler Warning (level 1) C4251"
4-
ms.date: 02/22/2022
4+
ms.date: 12/01/2023
55
f1_keywords: ["C4251"]
66
helpviewer_keywords: ["C4251"]
7-
ms.assetid: a9992038-f0c2-4fc4-a9be-4509442cbc1e
87
---
98
# Compiler Warning (level 1) C4251
109

1110
> '*type*' : class '*type1*' needs to have dll-interface to be used by clients of class '*type2*'
1211
1312
## Remarks
1413

14+
This warning happens if a class is marked with `__declspec(dllexport)` or `__declspec(dllimport)` and a nonstatic data member that is a member of the class or a member of one of its base classes, has a type that is a class type that isn't marked with `__declspec(dllexport)` or `__declspec(dllimport)`. See [Example](#example).
15+
1516
To minimize the possibility of data corruption when exporting a class declared as [`__declspec(dllexport)`](../../cpp/dllexport-dllimport.md), ensure that:
1617

1718
- All your static data is accessed through functions that are exported from the DLL.
18-
1919
- No inlined methods of your class can modify static data.
20-
2120
- No inlined methods of your class use CRT functions or other library functions that use static data. For more information, see [Potential errors passing CRT objects across DLL boundaries](../../c-runtime-library/potential-errors-passing-crt-objects-across-dll-boundaries.md).
22-
2321
- No methods of your class (whether inlined or not) can use types where the instantiation in the EXE and DLL have static data differences.
2422

25-
You can avoid issues when exporting a class from a DLL: Define your class to have virtual functions, a virtual destructor, and functions to instantiate and delete objects of the type. You can then just call virtual functions on the type.
23+
You can avoid issues when exporting a class from a DLL by:
2624

27-
C4251 can be ignored if your class is derived from a type in the C++ Standard Library, you're compiling a debug release (**`/MTd`**), and where the compiler error message refers to `_Container_base`.
25+
- Defining your class to have virtual functions.
26+
- Defining a virtual destructor.
27+
- Defining functions to instantiate and delete instances of the type.
2828

29-
## Example
29+
You can ignore C4251 if your class is derived from a type in the C++ Standard Library, you're compiling a debug release (**`/MTd`**), and the compiler error message refers to `_Container_base`.
30+
31+
Think carefully about adding `__declspec(dllexport)` or `__declspec(dllimport)` to a class because it's almost always not the right choice and it can make maintenance more difficult because it makes changing implementation details harder.
3032

31-
This sample exports a specialized class `VecWrapper` derived from `std::vector`.
33+
## Example
3234

3335
```cpp
3436
// C4251.cpp
35-
// compile with: /EHsc /MTd /W2 /c
37+
// Compile with /std:c++20 /EHsc /W2 /c C4251.cpp
38+
#include <vector>
39+
40+
class __declspec(dllexport) X
41+
{
42+
public:
43+
X();
44+
~X();
45+
46+
void do_something();
47+
48+
private:
49+
void do_something_else();
50+
std::vector<int> data; // warning c4251
51+
};
52+
```
53+
54+
To fix this warning, don't mark the class with `__declspec(dllexport)` or `__declspec(dllimport)`. Instead, only mark the methods that are used directly by a client. For example:
55+
56+
```cpp
57+
// C4251_fixed.cpp
58+
// Compile with /std:c++20 /EHsc /W2 /c C4251-fixed.cpp
3659
#include <vector>
37-
using namespace std;
38-
class Node;
39-
class __declspec(dllexport) VecWrapper : vector<Node *> {}; // C4251
60+
61+
class X
62+
{
63+
public:
64+
__declspec(dllexport) X();
65+
__declspec(dllexport) ~X();
66+
67+
__declspec(dllexport) void do_something();
68+
69+
private:
70+
void do_something_else();
71+
std::vector<int> data;
72+
};
4073
```

docs/sanitizers/asan-known-issues.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "AddressSanitizer known issues"
33
description: "Technical description of the AddressSanitizer for Microsoft C/C++ known issues."
4-
ms.date: 04/15/2022
4+
ms.date: 12/1/2023
55
helpviewer_keywords: ["AddressSanitizer known issues"]
66
---
77

@@ -23,10 +23,11 @@ These options and functionality are incompatible with [`/fsanitize=address`](../
2323
- [C++ AMP](../parallel/amp/cpp-amp-overview.md) is unsupported, and should be disabled.
2424
- [Universal Windows Platform](../cppcx/universal-windows-apps-cpp.md) (UWP) applications are unsupported.
2525
- [Special case list](https://clang.llvm.org/docs/SanitizerSpecialCaseList.html) files are unsupported.
26+
- [MFC](../mfc/mfc-concepts.md) using the optional [`alloc_dealloc_mismatch`](error-alloc-dealloc-mismatch.md) runtime option is unsupported.
2627

2728
## Standard library support
2829

29-
The MSVC standard library (STL) is partially enlightened to understand the AddressSanitizer and provide additional checks. For more information, see [container-overflow error](./error-container-overflow.md).
30+
The MSVC standard library (STL) is partially enlightened to understand the AddressSanitizer and provide other checks. For more information, see [container-overflow error](./error-container-overflow.md).
3031

3132
When annotations are disabled or in versions without support, AddressSanitizer exceptions raised in STL code do still identify true bugs. However, they aren't as precise as they could be.
3233

@@ -56,7 +57,7 @@ int main() {
5657

5758
## Windows versions
5859

59-
As there are dependencies with specific Windows versions, support is focused on Windows 10. MSVC AddressSanitizer was tested on 10.0.14393 (RS1), and 10.0.21323 (pre-release insider build). [Report a bug](https://aka.ms/feedback/report?space=62) if you run into issues.
60+
As there are dependencies with specific Windows versions, support is focused on Windows 10. MSVC AddressSanitizer was tested on 10.0.14393 (RS1), and 10.0.21323 (prerelease insider build). [Report a bug](https://aka.ms/feedback/report?space=62) if you run into issues.
6061

6162
## Memory usage
6263

@@ -68,7 +69,7 @@ The *`clang_rt.asan*.dll`* runtime files are installed next to the compilers in
6869

6970
## Custom property sheet support
7071

71-
The Property Manager window in the Visual Studio IDE allows you to add custom *`.props`* files to your projects. Even though the **Enable Address Sanitizer** property (`<EnableASAN>`) is shown, it's not honored by the build. That's because the custom *`.props`* files get included after *`Microsoft.cpp.props`*, which uses the `<EnableASAN>` value to set other properties.
72+
The Property Manager window in the Visual Studio IDE allows you to add custom *`.props`* files to your projects. Even though the **Enable Address Sanitizer** property (`<EnableASAN>`) is shown, the build doesn't honor it. That's because the custom *`.props`* files get included after *`Microsoft.cpp.props`*, which uses the `<EnableASAN>` value to set other properties.
7273

7374
As a workaround, you can create a *`Directory.Build.props`* file in the root of your project to define the `<EnableASAN>` property. For more information, see [Customize C++ builds](/visualstudio/msbuild/customize-your-build#customize-c-builds).
7475

0 commit comments

Comments
 (0)