Created
August 12, 2010 09:44
-
-
Save sinfu/520645 to your computer and use it in GitHub Desktop.
Uniqueのデストラクタは安全?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Container | |
{ | |
struct // Unique | |
{ | |
// Unique のデストラクタ… | |
// これが呼ばれたとき,Unique 自身のメモリ領域 (Container のメモリ領域) は生きてる? 死んでる? | |
// | |
// 「Container のメモリ領域開放 → Unique の破棄」というシナリオはあり得る? 逆はあっても…. | |
// | |
~this() | |
{ | |
// payload のデストラクタを呼ぶ | |
__dtor(); | |
} | |
struct // payload (何かのクラスインスタンス) | |
{ | |
void* vtbl; | |
void* monitor; | |
// | |
int member_1; | |
int[] member_2; // GC で確保したメモリ | |
int* member_3; // malloc で確保したメモリ | |
//... | |
// payload のデストラクタ… | |
// vtbl, monitor, member_2 の参照先はともかく,変数自体は生きてるのでは? | |
void __dtor() | |
{ | |
// Unique 自身のメモリ領域が生きてるならば,ポインタ変数 member_3 は生きていて,正しいアドレスを指している | |
core.stdc.stdlib.free(member_3); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment