Objective-C++ クラスでオブジェクトを作成すると、動作に違いが見られます。
DictionaryWith と numberWith を使用してNSDictionary
包含オブジェクトを作成すると、オブジェクトが解放されることはありません。NSNumber
と を使用してそれらを作成するalloc
とinitWith
、問題なくクリーンアップされます。
これは、同じプロジェクトの Objective-C クラスでは見られません。プロジェクトで ARC が有効になっています。CFNumber
Xcode 4.5.2 で Allocations プロファイリング ツールを使用して、と __の "# Living" 値を調べていNSDictionaryl
ます。
// These objects will NOT be released.
NSDictionary* dict1 = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInt:val1], @"val1",
[NSNumber numberWithUnsignedInt:val2], @"val2",
[NSNumber numberWithUnsignedInt:val3], @"val3",
nil];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]
postNotificationName:KEY_NET_STATS_VIEW_UDATE object:nil userInfo:dict1];
});
// These objects *will* be released.
NSDictionary* dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSNumber alloc] initWithUnsignedInt:val1], @"val1",
[[NSNumber alloc] initWithUnsignedInt:val2], @"val2",
[[NSNumber alloc] initWithUnsignedInt:val3], @"val3",
nil];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]
postNotificationName:KEY_NET_STATS_VIEW_UDATE object:nil userInfo:dict2];
});
alloc/initWith を使用してコードを記述しても問題はありませんが、その違いの理由を理解したいと思います。私が読んだものはすべて、ARCの下で同等であるべきだと言っています。
このコードが呼び出されたときのスタック トレース。以下はすべて C++ です。
#0 0x001fbbe4 in ItRtpSessionManageriOS::OnItRtpOutgoingStatsUpdate(ItRtpSession&, ItRtpSessionManager::ItRtpStats const&)
#1 0x0007018a in CSceApp::OnItRtpOutgoingStatsUpdate(ItRtpSession&, ItRtpSessionManager::ItRtpStats const&)
#2 0x0006b808 in ItRtpSession::CallStatsUpdateCallback(ItRtpSessionManager::ItRtpStats const&)
#3 0x0006ab1e in ItRtpSessionSharedCommXYZ::UpdateOutgoingStats(unsigned long, unsigned long)
#4 0x0006a958 in ItRtpSessionSharedCommXYZ::Update(unsigned int, unsigned int)
#5 0x000764ca in CSceApp::EvTimerServiceMgrAwaken(bool, unsigned int, void*)
#6 0x00076908 in non-virtual thunk to CSceApp::EvTimerServiceMgrAwaken(bool, unsigned int, void*)
#7 0x002a0134 in xyz::CServicingThread::Activate(unsigned long long, bool*)
#8 0x0029fb98 in xyz::CServicingThread::Behavior()
#9 0x0029fc34 in non-virtual thunk to xyz::CServicingThread::Behavior()
#10 0x002578de in xyz::CAliveObj::StartMechanism(void*)
#11 0x00259f9e in xyz::CThread::ThreadEntry(void*)
#12 0x348b5310 in _pthread_start ()
#13 0x348b51d8 in thread_start ()