"Scalar deleting destructor" error
-
I have code that looks like: CArray ArrayOfObjPtrs; ObjPtr op=new Obj(); ArrayOfObjPtrs.Add(op); Then in another function called later in the same thread, I do: delete ArrayOfObjPtrs.GetAt(0); // free the object ** run-time error on this line ** ArrayOfObjPtrs.RemoveAt(0); the Run-time error is in MSVCRTD, but the last call of mine is on the stack right before it, is "Obj::`scalar deleting desctructor' (unsigned int 1) + 56 bytes I get a Debug Asseertion: dbgdel.cpp Line 47, Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Can anyone please tell me what is going on? Thanks soooo much! PS. Commenting out the 'delete' line results in a memory leak
-
I have code that looks like: CArray ArrayOfObjPtrs; ObjPtr op=new Obj(); ArrayOfObjPtrs.Add(op); Then in another function called later in the same thread, I do: delete ArrayOfObjPtrs.GetAt(0); // free the object ** run-time error on this line ** ArrayOfObjPtrs.RemoveAt(0); the Run-time error is in MSVCRTD, but the last call of mine is on the stack right before it, is "Obj::`scalar deleting desctructor' (unsigned int 1) + 56 bytes I get a Debug Asseertion: dbgdel.cpp Line 47, Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Can anyone please tell me what is going on? Thanks soooo much! PS. Commenting out the 'delete' line results in a memory leak
Probabaly you could remove the element first and then delete it next using an intermediatery temp variable.
-
Probabaly you could remove the element first and then delete it next using an intermediatery temp variable.
What effect would that have?
-
I have code that looks like: CArray ArrayOfObjPtrs; ObjPtr op=new Obj(); ArrayOfObjPtrs.Add(op); Then in another function called later in the same thread, I do: delete ArrayOfObjPtrs.GetAt(0); // free the object ** run-time error on this line ** ArrayOfObjPtrs.RemoveAt(0); the Run-time error is in MSVCRTD, but the last call of mine is on the stack right before it, is "Obj::`scalar deleting desctructor' (unsigned int 1) + 56 bytes I get a Debug Asseertion: dbgdel.cpp Line 47, Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Can anyone please tell me what is going on? Thanks soooo much! PS. Commenting out the 'delete' line results in a memory leak
How does the class ObjPtr look? Looks to me that there is some sort of a casting problem. I feel that ArrayOfObjPtrs.GetAt(0) is not returning Obj* for some reason.
-
How does the class ObjPtr look? Looks to me that there is some sort of a casting problem. I feel that ArrayOfObjPtrs.GetAt(0) is not returning Obj* for some reason.
new() returns a pointer This pointer to the object, not the object itself, is stored in the CArray
-
What is ObjPtr is it a class or there was a typo in the original mail ObjPtr op = new Obj(); or is it Obj* op = new Obj(); How does you ObjPtr class look?
Im my example, they both are the same. ObjPtr is a Obj * Sorry for the abiguity.
-
new() returns a pointer This pointer to the object, not the object itself, is stored in the CArray
What is ObjPtr is it a class or there was a typo in the original mail ObjPtr op = new Obj(); or is it Obj* op = new Obj(); How does you ObjPtr class look?
-
Im my example, they both are the same. ObjPtr is a Obj * Sorry for the abiguity.
Try telling the CArray what kind of thing you will be storing in it. For example if you were storing pointers to CString objects, you would define it like this: CArray myArray;
-
Im my example, they both are the same. ObjPtr is a Obj * Sorry for the abiguity.
-
I have code that looks like: CArray ArrayOfObjPtrs; ObjPtr op=new Obj(); ArrayOfObjPtrs.Add(op); Then in another function called later in the same thread, I do: delete ArrayOfObjPtrs.GetAt(0); // free the object ** run-time error on this line ** ArrayOfObjPtrs.RemoveAt(0); the Run-time error is in MSVCRTD, but the last call of mine is on the stack right before it, is "Obj::`scalar deleting desctructor' (unsigned int 1) + 56 bytes I get a Debug Asseertion: dbgdel.cpp Line 47, Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Can anyone please tell me what is going on? Thanks soooo much! PS. Commenting out the 'delete' line results in a memory leak
Hope u're not doing something like: delete this; within the destructor. It may have destroyed the pointer when u exited the function within which u did the allocation. That's a rather strange characteristic of C++. If that's not the case then u may have destroyed the object somewhere else without realizing it,otherwise there's no reason the above code should throw that exception.