Sign in to follow this  
PumpkinPieman

[.net] Debug Assertion Failed? Hunh?

Recommended Posts

PumpkinPieman    382
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[i];
					iter->Tag = cloneLSF(iter->Tag);
					for(int j = 0; j < iter->Nodes->Count; j++){
						iter = copyData->Nodes->Item[i]->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

Share this post


Link to post
Share on other sites
Palidine    1315
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/unset
for(int i = 0; i < iter->Nodes->Count; i++){ //<---- if iter is NULL/unset this is accessing invalid memory
iter = copyData->Nodes->Item[i]; // <--- 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

Share this post


Link to post
Share on other sites
PumpkinPieman    382
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)

Share this post


Link to post
Share on other sites
PumpkinPieman    382
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()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this