With C++, I get pointer with 0xcdcdcdcd when creat

2019-04-16 20:41发布

问题:

This question already has an answer here:

  • When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete? 9 answers

----------------EDIT-----------------------

I was grabbing the wrong value for comparison, the cdcdcdcd was coming from somewhere else. I still have my methods throwing exceptions before they are even reached, but my problem lies elsewhere, I wish there was a way to just "unpost" my original question. Thanks for the help.

----------------EDIT-----------------------

I have a class (MyClass) I have inherited from some third party class (which in turn derives from direct show classes - from CBaseFilter if this matters). I write this code:

MyClass* val = NULL;
val = new MyClass(); // Create an instance of the class.

Attempting to make method calls results in a thrown exception. With a little sleuthing I found that when I dereference val (val itself seems valid... something like 0x0777fc90), it contains 0xcdcdcdcd. I looked around and it seems that this might indicate that the memory has been allocated on the heap, but not initialized.

What does that mean?! How can the call to new succeed (val != NULL) and yet the memory isn't initialized enough to have a pointer to it? Is it possible that something went wrong with some of the base class initialization? And if so - what am I looking for?

回答1:

0xCDCDCDCD is a debugging value that comes from the bowels of the C runtime library. When you allocate a block of memory in a debug build, it's initialized to this spurious value in the hope of catching bugs. 0xCDCDCDCD is non-NULL and is never a valid memory pointer.

Is there an HRESULT that passes up through from DirectShow that you can check?

Try calling WINAPI function GetLastError() and see what that shows.