[.net] Debug Assertion Failed? Hunh?

Started by
3 comments, last by PumpkinPieman 18 years ago
I've been trying to make a copy and paste method for a treeview where if you copy it makes a clone of the tree ( a simple node->Clone() ) However, each node has a tag value that is a pointer to an attached structure. Since Object* doesn't have a public clone method I created one for each of the structures that returns this->MemberwiseClone(), and it works fine until the application is closed. Then I get this nice nasty error. Currently this is the code I use to do the copying.

				if(sn != NULL)
					copyData = __try_cast<TreeNode*>(sn->Clone());

				TreeNode* iter = copyData;

				for(int i = 0; i < iter->Nodes->Count; i++){
					iter = copyData->Nodes->Item;
					iter->Tag = cloneLSF(iter->Tag);
					for(int j = 0; j < iter->Nodes->Count; j++){
						iter = copyData->Nodes->Item->Nodes->Item[j];
						iter->Tag = cloneLSF(iter->Tag);
					}
				}

// note all objects inherit LSF_TYPE

System::Object* cloneLSF(System::Object* obj)
{
	if(obj != NULL){
		try{
			return __try_cast<LSF_TYPE*>(obj)->Clone();
		}
		catch(System::Exception*){}
	}
	return NULL;
}

I've tried different ways of trying to clone them, but I get a similar error so I'm not quite sure what I'm doing wrong. Thanks
Advertisement
You've got a ton of places where things can go wrong:

if(sn != NULL) // <---- if sn is NULL then coypData will remain NULL/unset    copyData = __try_cast<TreeNode*>(sn->Clone());    TreeNode* iter = copyData;  // <---- if sn was NULL above you've just set iter to NULL/unsetfor(int i = 0; i < iter->Nodes->Count; i++){   //<---- if iter is NULL/unset this is accessing invalid memory    iter = copyData->Nodes->Item; // <--- if copyData is NULL/unset, this is accessing invalid memory    iter->Tag = cloneLSF(iter->Tag); // <--- cloneLSF can return NULL (don't know if this can cause problems)


Are you running this in a debugger so you can walk around the callstack when that assert is thrown to see what's going on? Also, I trust that you are setting all your pointers to NULL when you create them? i.e. copyData should be NULL or a known valid value before the if (sn != NULL) line.

-me
the if(sn != NULL) line is pointless, because I already check that and make sure it can't execute that block anyway.

One of the big problems I see with the clone is this

NextNode <error: an exception of type: 0x01557810 occurred> System.Windows.Forms.TreeNode
Parent <undefined value> System.Windows.Forms.TreeNode
PrevNode <undefined value> System.Windows.Forms.TreeNode

Why is it that the cloned value has a NULL parent and prevNode, but the nextNode throws an exception? Shouldn't it be NULL as well? (I can't set it there for I can't properly loop through them)
it would be a good idea to tell us, which line is line 52 :)
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
Quote:Original post by orbano
it would be a good idea to tell us, which line is line 52 :)

Well, that's not really one of my files. >.>

I've got around the debug assertion failed by using the clone() getData() and setData() methods to make a new structure and set the contents of this stucture and return the new one. So I guess I shouldn't have used MemberwiseClone()

This topic is closed to new replies.

Advertisement