I have a strange 'destructor' Behavior in C++
Here is the code I call :
_log->OnCommit(); delete _log; The problem is that when I call "delete _log;" it crash because the variable 'Entries' is invalid !!!!
Do you know why ?
Here is my class code :
struct TransactionLogEntry { DependencyObject* Object; bool IsAttached; bool IsDeleted; bool IsUpdated; }; class TransactionLog { public: TransactionLog(); ~TransactionLog(); void OnCommit(); map<DependencyObject*, TransactionLogEntry*> Entries; }; void TransactionLog::OnCommit() { map<DependencyObject*, TransactionLogEntry*>::iterator it; for(it = Entries.begin(); it != Entries.end(); it++) { TransactionLogEntry* entry = (TransactionLogEntry*)(*it).second; if (entry->IsDeleted) delete entry->Object; delete entry; } Entries.clear(); } TransactionLog::~TransactionLog() { map<DependencyObject*, TransactionLogEntry*>::iterator it; for(it = Entries.begin(); it != Entries.end(); it++) { TransactionLogEntry* entry = (TransactionLogEntry*)(*it).second; delete entry; } Entries.clear(); }
onCommitreturns correctly? have you checked it under a debugger?(*it).secondand it's calledit->second.