Original Post
I´m coding a 2D engine using DirectX 6.1. One of the classes is in charge of creating the several DD Objects needed such as surfaces, clipper, palette, etc. Something very weird happened today when I was testing this class: one of its methods is
int CVideoObjectsFactory::BuildDDObject(LPDIRECTDRAW4 lpNewObj);
Basically I pass a pointer to an IDIRECTDRAW4 interface and inside the method it is validated:
LPDIRECTDRAW lpDD = NULL;
hDDRetVal = DirectDrawCreate(NULL,&lpDD,NULL);
// Error handling.
lpDD->QueryInterface(IID_IDirectDraw4,(LPVOID*)&lpNewObj);
// More Error handling.
lpDD->Release();
lpDD = NULL;
iDDObject++;
iObjectsBuilt++;
return BUILT_SUCCESSFUL;
As I was saying, I tried this method and the compiler flagged an unhandled exception one line of code after the one that makes the call to this method (which uses the just created DDraw object). The watch window revealed that the pointer I passed to the method was NULL (that is, it the BuildDDObject method returned and the pointer was NULL). All my problem got solved by adding a simple character to the function declaration and definition: ''&''.
int CVideoObjectsFactory::BuildDDObject(LPDIRECTDRAW4 &lpNewObj);
But I don´t understand why. I mean, isn´t LPDIRECTDRAW4 a pointer to the IDIRECTDRAW4 interface? And if I pass a pointer to a function, doesn´t its value change? Can somebody explain me what happened here? Thanks in advance!!