Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Im getting runtime break for object pStg stating "Run-Time Check Failure #3 - The variable 'pStg' is being used without being initialized.". And im not getting this error for pIstor object which is used earlier than pStg in the code. what is the reason??

EXTERN_C void wmain()
{
   HRESULT hr = S_OK;
  IStorage *pIstor;
  IStorage *pIstor1;
   WCHAR *pwszError = L"";
	
IStream* pStream;

   StgCreateDocfile(L"sumit444",STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE, 0, &pIstor);
   hr=pIstor->CreateStorage(L"sumit444",STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,0, 0, &pIstor1);
	       
if (pIstor->CreateStream(L"MyStreamName",STGM_CREATE |STGM_READWRITE | STGM_SHARE_EXCLUSIVE,0, 0, &pStream) == S_OK)
{
  //some code 
}
	
SetCurrentDirectory(L"E:");

IStorage* pStg;

hr=StgCreateStorageEx(L"E",STGM_CREATE | STGM_READWRITE | STGM_TRANSACTED,STGFMT_STORAGE,0,0,0,IID_IStorage,(void**)pStg);
.
.// some code follows

Thanx in advance

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 13-Mar-11 11:45am
v2

MSDN[^] says:
WINOLEAPI StgCreateStorageEx(
  __in   const WCHAR *pwcsName,
  __in   DWORD grfMode,
  __in   STGFMT stgfmt,
  __in   DWORD grfAttrs,
  __in   STGOPTIONS *pStgOptions,
  __in   PSECURITY_DESCRIPTOR *pSecurityDescriptor,
  __in   REFIID riid,
  __out  void **ppObjectOpen
);
Which means that your pStg is an OUT parameter: I.e A pointer to an interface pointer variable that receives a pointer for an interface on the new storage object. pStg is not initialized to point anywhere, so you get an error!
 
Share this answer
 
Comments
MartinTaylor 13-Mar-11 17:56pm    
So Sir what correction should i do??
OriginalGriff 14-Mar-11 4:14am    
You could assign the address of the address of a IStorage instance...
Just a thought?
The problem should be "(void**)pStg" because you have to pass address of pStg. Try using "reinterpret_cast<void**> &pStg".
hr=StgCreateStorageEx(L"E",STGM_CREATE | STGM_READWRITE | STGM_TRANSACTED,STGFMT_STORAGE,0,0,0,IID_IStorage,reinterpret_cast<void**> &pStg);
 
Share this answer
 
Comments
MartinTaylor 14-Mar-11 2:13am    
after the modification you mentioned i am getting runtime error as
"Unhandled exception at 0x01c30620 in testStorage1.exe: 0xC0000005: Access violation."
So how to handle this exception?
Ozer Karaagac 14-Mar-11 6:52am    
Sorry, I made a typo by forgetting parenthesis around &pStg. It should be "reinterpret_cast<void**> (&pStg)". (may be, interference by paste operation, overlooked). Anyway, It must have been corrected by you in order to compile above line without error. Additionally, initializing pointers with NULL is a good practice (pStg = NULL);

When it comes to run-time error. It seems to be a pointer points to an invalid memory address. I don't think that my correction can cause this error. May be, subsequent lines. If I were you, I would try to determine which line caused the error by stepping through the code (by F10 key). Then, you can past that line here. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900