Skip to content

Commit b1c2868

Browse files
Fea, RefPtr中禁止手工调用AddRef/Release,避免潜在引用计数泄漏
1 parent 815e2e8 commit b1c2868

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

include/YY/Base/IO/File.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ namespace YY
324324

325325
if (bSkipCompletionNotificationOnSuccess)
326326
{
327-
_pIoTask->Release();
327+
_pIoTask.Get()->Release();
328328
}
329329
else
330330
{
@@ -345,7 +345,7 @@ namespace YY
345345
{
346346
// 失败!
347347
_pIoTask->OnComplete(_lStatus);
348-
_pIoTask->Release();
348+
_pIoTask.Get()->Release();
349349
return _lStatus;
350350
}
351351
}
@@ -371,7 +371,7 @@ namespace YY
371371

372372
if (bSkipCompletionNotificationOnSuccess)
373373
{
374-
_pIoTask->Release();
374+
_pIoTask.Get()->Release();
375375
}
376376
else
377377
{
@@ -392,7 +392,7 @@ namespace YY
392392
{
393393
// 失败!
394394
_pIoTask->OnComplete(_lStatus);
395-
_pIoTask->Release();
395+
_pIoTask.Get()->Release();
396396
return _lStatus;
397397
}
398398
}
@@ -573,7 +573,7 @@ namespace YY
573573

574574
if (bSkipCompletionNotificationOnSuccess)
575575
{
576-
_pIoTask->Release();
576+
_pIoTask.Get()->Release();
577577
}
578578
else
579579
{
@@ -594,7 +594,7 @@ namespace YY
594594
{
595595
// 失败!
596596
_pIoTask->OnComplete(_lStatus);
597-
_pIoTask->Release();
597+
_pIoTask.Get()->Release();
598598
return _lStatus;
599599
}
600600
}

include/YY/Base/Memory/RefPtr.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ namespace YY
112112
}
113113
};
114114

115+
template <class T>
116+
class NoAddRefReleaseOnRefPtr :public T
117+
{
118+
private:
119+
static uint32_t __YYAPI AddRef()
120+
{
121+
static_assert(false, "RefPtr上主动调用AddRef做什么?");
122+
return 0;
123+
}
124+
125+
static uint32_t __YYAPI Release()
126+
{
127+
static_assert(false, "RefPtr上主动调用Release做什么?");
128+
return 0;
129+
}
130+
};
131+
115132
template<typename _Type>
116133
class RefPtr
117134
{
@@ -240,9 +257,9 @@ namespace YY
240257
return p;
241258
}
242259

243-
_Ret_maybenull_ _Type* __YYAPI operator->() const noexcept
260+
_Ret_maybenull_ NoAddRefReleaseOnRefPtr<_Type>* __YYAPI operator->() const noexcept
244261
{
245-
return p;
262+
return reinterpret_cast<NoAddRefReleaseOnRefPtr<_Type>*>(p);
246263
}
247264

248265
template<typename... Args>

0 commit comments

Comments
 (0)