1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | // |
5 | // StringCopyHolder.cpp |
6 | // |
7 | |
8 | // |
9 | // This is the implementation of a simple holder for a copy of a string. |
10 | // |
11 | // ====================================================================================== |
12 | |
13 | |
14 | // Initialize to Null. |
15 | StringCopyHolder::StringCopyHolder() |
16 | { |
17 | m_szData = NULL; |
18 | } |
19 | |
20 | // Dtor to free memory. |
21 | StringCopyHolder::~StringCopyHolder() |
22 | { |
23 | Clear(); |
24 | } |
25 | |
26 | // Reset the string to NULL and free memory |
27 | void StringCopyHolder::Clear() |
28 | { |
29 | if (m_szData != NULL) |
30 | { |
31 | delete [] m_szData; |
32 | m_szData = NULL; |
33 | } |
34 | } |
35 | |
36 | //--------------------------------------------------------------------------------------- |
37 | // |
38 | // Allocate a copy of the incoming string and assign it to this holder. |
39 | // pStringSrc can be NULL or a pointer to a null-terminated string. |
40 | // |
41 | // Arguments: |
42 | // pStringSrc - string to be duplicated |
43 | // |
44 | // Returns: |
45 | // S_OK on success. That means it succeeded in allocating and copying pStringSrc. |
46 | // If the incoming string is NULL, then the underlying string will be NULL as welll. |
47 | // Callers may want to assert that IsSet() is true if they don't expect a NULL string. |
48 | // |
49 | // E_OUTOFMEMORY on failure. Only happens in an OOM scenario. |
50 | // |
51 | // Notes: |
52 | // Since this function is a callback from DAC, it must not take the process lock. |
53 | // If it does, we may deadlock between the DD lock and the process lock. |
54 | // If we really need to take the process lock for whatever reason, we must take it in the DBI functions |
55 | // which call the DAC API that ends up calling this function. |
56 | // See code:InternalDacCallbackHolder for more information. |
57 | // |
58 | |
59 | HRESULT StringCopyHolder::AssignCopy(const WCHAR * pStringSrc) |
60 | { |
61 | if (m_szData != NULL) |
62 | { |
63 | Clear(); |
64 | } |
65 | |
66 | if (pStringSrc == NULL) |
67 | { |
68 | m_szData = NULL; |
69 | } |
70 | else |
71 | { |
72 | SIZE_T cchLen = wcslen(pStringSrc) + 1; |
73 | m_szData = new (nothrow) WCHAR[cchLen]; |
74 | if (m_szData == NULL) |
75 | { |
76 | _ASSERTE(!"Warning: Out-of-Memory in Right Side. This component is not robust in OOM sccenarios." ); |
77 | return E_OUTOFMEMORY; |
78 | } |
79 | |
80 | wcscpy_s(m_szData, cchLen, pStringSrc); |
81 | } |
82 | return S_OK; |
83 | } |
84 | |