Alt Tab + Restoring Textures

Started by
23 comments, last by utilae 19 years, 2 months ago
Quote:So what would be the purpose of ever using D3DPOOL_DEFAULT, if D3DPOOL_SYSMEM and D3DPOOL_MANAGED take care of all the resource releasing/reaquiring for you?

Theres a better discussion of this in the SDK helpfiles somewhere...

But it comes down to who (and how) you control resources. D3DPOOL_MANAGED is swapped between memory as D3D sees fit (if you don't use it for a while it might get paged out of VRAM back to AGP/RAM). This can work fine - but obviously, if you know more about the problem-space and can optimally control when it gets paged-in/out you can (at least try to) avoid delays when D3D has it stored in the wrong place and has to page it back into VRAM before the draw call can be honoured...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Advertisement
My textures and vertex buffer are D3DPOOL_MANAGED.

Looking around I couldn't see anything that is D3DPOOL_DEFAULT.

Creating D3D9 object, presentation parameters and d3d9 device, they don't seem to have any 'pool' setting.

My 2d ortho matrix doesn't have any 'pool' setting.

I don't know why it's not working. Tell me about this safe release thing. Am I mean't to release direct3d9 device and then reload it when you alt tab?

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Perhaps you could show us the line that causes the exception?
I'm using visual studio .net 2003.

When I try debug->start, my app crashes. I can build it and run it from the debug directory. The app runs fine, but when I alt tab, then put focus back on the app, then it crashes (application error-memory could not be written) and asks if I want to debug. I try that but it says the Just In Time Debugging Failed (unspecified error). So I can't really go to the exact line.

I'll post more code (showing how i set everything up, so maybe there's something in D3DPOOL_DEFAULT that I have not picked up):
////setup d3d9 stuff//Create an IDirect3D9 Objectg_pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);//set the presentation parametersZeroMemory(&g_d3dpp, sizeof(g_d3dpp));g_d3dpp.BackBufferWidth = 1024;g_d3dpp.BackBufferHeight = 768;g_d3dpp.BackBufferCount = 1;g_d3dpp.BackBufferFormat = D3DFMT_R5G6B5;g_d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE; //No multi-samplingg_d3dpp.MultiSampleQuality=0;  g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;g_d3dpp.hDeviceWindow = hWndMain;g_d3dpp.Windowed = false;g_d3dpp.EnableAutoDepthStencil = true;//z bufferg_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;//z bufferg_d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;//create an IDirect3DDevice9 object, try hardware/hardwareg_pD3D9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWndMain,D3DCREATE_HARDWARE_VERTEXPROCESSING,&g_d3dpp,&g_pD3DDevice9);


//setup matrixD3DXMATRIX Ortho2D;	D3DXMATRIX Identity;	//1024x768 2d perspective (0 is near, 1 is far)D3DXMatrixOrthoLH(&Ortho2D,-1024,768,0.0f,1.0f);D3DXMatrixIdentity(&Identity);g_pD3DDevice9->SetTransform(D3DTS_PROJECTION,&Ortho2D);g_pD3DDevice9->SetTransform(D3DTS_WORLD,&Identity);g_pD3DDevice9->SetTransform(D3DTS_VIEW,&Identity);


//render statesg_pD3DDevice9->SetRenderState(D3DRS_ALPHABLENDENABLE,true);//alphag_pD3DDevice9->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);//alphag_pD3DDevice9->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);//alphag_pD3DDevice9->SetRenderState(D3DRS_LIGHTING,false);//lighting offg_pD3DDevice9->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);//turn off cullingg_pD3DDevice9->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);//z buffer g_pD3DDevice9->SetRenderState(D3DRS_ZWRITEENABLE,true);//z buffer g_pD3DDevice9->SetRenderState(D3DRS_ZFUNC,D3DCMP_LESSEQUAL);//z buffer 


////VERTEX BUFFER//create d3d9 vertex bufferg_pD3DDevice9->CreateVertexBuffer(byte_count,D3DUSAGE_WRITEONLY,g_dwFVF,D3DPOOL_MANAGED,&g_pVertexBuffer,NULL);	//lock vertex buffervoid *vb_vertices;g_pVertexBuffer->Lock(0,0,&vb_vertices,0);//fill the Vertex Buffermemcpy(vb_vertices,data,byte_count);g_pVertexBuffer->Unlock();


//texture is not already loaded, so now a texture can be loadedD3DSURFACE_DESC surfaceDesc;LOADEDTEXTURE *newTexture= new LOADEDTEXTURE;	//load textureD3DXCreateTextureFromFileEx(g_pD3DDevice9,sFilename.c_str(),D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,0,0,&newTexture->m_texture);newTexture->m_texture->GetLevelDesc(0, &surfaceDesc);//store textures filename, height and widthnewTexture->m_sFilename=sFilename;newTexture->m_nHeight=surfaceDesc.Height;newTexture->m_nWidth=surfaceDesc.Width;//put texture into listm_lstMainTextureList.push_back(newTexture);


//freeing and loading of volatile resources when app looses or gains focus//see 1st post for that bit of codevoid InitVolatileResources(){	//Init internal resources	g_pFont->OnResetDevice();}void FreeVolatileResources(){	//Free up some internal resources	g_pFont->OnLostDevice();}

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Right, I can't see anything glaringly obvious from your source code snippets. The only thing that could be suspect (and this is as much from it being a black box as anything else) is your LOADEDTEXTURE pointer that you store in an STL container...

Are you sure this isn't just a standard memory r/w error? are these pointers at any point getting invalidated?

Some better trace information would be very useful here - Isolating a function/line will probably throw this problem wide open [smile].

You say you can't run it through VStudio's debugger - is it a general crash (i.e. a bug in vstudio/bad config) or is the debugger telling you, from the outset, that your program doesn't make the world happy..?

Do you have any trace-file information? can you do a "cout" or "printf" style statement to any logger so as to try and gain an idea of what line is failing?

Have you got the debug runtimes set to maximum output, and run it's monitoring application in the console (so you can see the debug spew without VStudio)?

Sorry I can't be more specific!
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

When I run the app from the debug directory after building it it runs fine (except the alt tab thing).

When I go debug->start, the app just locks up and I have to end task it, I then break in the debugger and I get this:

'RTS Project.exe': Loaded 'D:\Chris\Programming\My Visual Studio Projects\RTS Project\Debug\RTS Project.exe', Symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\d3d9.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\d3d8thk.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\version.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\winmm.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\Program Files\Messenger Plus! 3\MsgPlusH.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\comdlg32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\SHLWAPI.DLL', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\oleaut32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.1612_x-ww_7c379b08\comctl32.dll', No symbols loaded.
'RTS Project.exe': Loaded 'C:\WINDOWS\system32\d3d9d.dll', No symbols loaded.
Direct3D9: :====> ENTER: DLLMAIN(00d816f0): Process Attach: 00000e40, tid=000008f0
Direct3D9: :====> EXIT: DLLMAIN(00d816f0): Process Attach: 00000e40
Direct3D9: (INFO) :Direct3D9 Debug Runtime selected.
D3D9 Helper: Enhanced D3DDebugging disabled; Application was not compiled with D3D_DEBUG_INFO
Direct3D9: (INFO) :======================= Hal HWVP device selected

Direct3D9: (INFO) :HalDevice Driver style 9

Direct3D9: :Subclassing window 0007057c
Direct3D9: :StartExclusiveMode
Direct3D9: :WM_DISPLAYCHANGE: 1024x768x16
Direct3D9: (WARN) :Ignoring redundant SetRenderState - 7

Direct3D9: (WARN) :Ignoring redundant SetRenderState - 14

Direct3D9: (WARN) :Ignoring redundant SetRenderState - 23

First-chance exception at 0x004bc16e in RTS Project.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
Unhandled exception at 0x004bc16e in RTS Project.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
The program '[3648] RTS Project.exe: Native' has exited with code 0 (0x0).

Anything useful there.

Here's my full texture code:
//the loadedtexture structurestruct LOADEDTEXTURE    {        IDirect3DTexture9 *m_texture;     //The texture        string m_sFilename;               //The filename of the texture        int m_nWidth;                      //Width of the texture        int m_nHeight;                     //Height of the texture    };//Load texture from filevoid CTextureManager::LoadTexture(string sFilename){	      //iterate through texture list      list<LOADEDTEXTURE*>::iterator itTextures=m_lstMainTextureList.begin();      while(itTextures!=m_lstMainTextureList.end())      {          if((*itTextures)->m_sFilename==sFilename)          //texture already loaded, so return          g_Log<<"LoadTexture("<<sFilename<<") ALREADY LOADED"<<endl;      }      //texture is not already loaded, so now a texture can be loaded      D3DSURFACE_DESC surfaceDesc;      LOADEDTEXTURE *newTexture= new LOADEDTEXTURE;	      //load texture      D3DXCreateTextureFromFileEx(g_pD3DDevice9,sFilename.c_str(),D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,0,0,&newTexture->m_texture);      newTexture->m_texture->GetLevelDesc(0, &surfaceDesc);      //store textures filename, height and width      newTexture->m_sFilename=sFilename;      newTexture->m_nHeight=surfaceDesc.Height;      newTexture->m_nWidth=surfaceDesc.Width;      //put texture into list      m_lstMainTextureList.push_back(newTexture);      //texture now loaded      g_Log<<"LoadTexture("<<sFilename<<") SUCCEEDED"<<endl;      return;}//Clear all texturesvoid CTextureManager::ClearTextures(void){		list<LOADEDTEXTURE*>::iterator itTextures;	//iterate through texture list    for(itTextures=m_lstMainTextureList.begin();itTextures!=m_lstMainTextureList.end();itTextures++)    {		//Release texture        if ((*itTextures)->m_texture)			(*itTextures)->m_texture->Release();        (*itTextures)->m_texture=NULL;        		//delete loadedtexture object        delete (*itTextures);    }    //Clear entire list    m_lstMainTextureList.clear();	g_Log<<endl<<"ClearTextures SUCCEEDED"<<endl;}//Set texturevoid CTextureManager::SetTexture(string sFilename){		//set iterator	list<LOADEDTEXTURE*>::iterator itTextures=m_lstMainTextureList.begin();	while(itTextures!=m_lstMainTextureList.end())	{		//found texture		if((*itTextures)->m_sFilename==sFilename)		{			//set active texture			g_pD3DDevice9->SetTexture(0,(*itTextures)->m_texture);			g_Log<<"SetTexture("<<sFilename<<") SUCCEEDED"<<endl;			return;		}	}}


Quote:
Have you got the debug runtimes set to maximum output, and run it's monitoring application in the console (so you can see the debug spew without VStudio)?

Debug runtimes are set to max output. How do I set debugging up porperly (any hints)? Also how do I set up this "monitoring application"?

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Quote:First-chance exception at 0x004bc16e in RTS Project.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
Unhandled exception at 0x004bc16e in RTS Project.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
The program '[3648] RTS Project.exe: Native' has exited with code 0 (0x0).

and
Quote:The app runs fine, but when I alt tab, then put focus back on the app, then it crashes (application error-memory could not be written)

I think you've just provided yourself a lead [smile].

It's useful to note that both the debug runtimes (for general C++/Win32) as well as the DX-Debug libraries are mostly slow because they validate/verify everything that happens. Retail releases of both assume either everything is okay, or that you (the programmer) know what is happening thus don't validate your code.

So, the fact that the debug runtimes are picking something up early on is a good indication that there is a bigger bug (or just another, hopefully similar, one) in your program that you've just been lucky not to see in a retail build...

I vaguely remember 0xcdcdcdcd being a special code - so I looked it up on google and found this page. About 1/3rd the way down there is this entry:
Quote:0xCDCDCDCD
Allocated in heap, but not initialized


So you've basically got some screwy pointers in your program that you're either not initializing at all - or by some case of flow-control you're skipping the code that does initialize them. You could, quite simply, test all pointers and addresses against 0xCDCDCDCD whenever they are used, referenced, declared - bit messy, but it'll work.

hth [grin]
Jack

[Edited by - jollyjeffers on February 15, 2005 1:23:19 PM]

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Just looking through your code:

//void CTextureManager::LoadTexture(string sFilename)while(itTextures!=m_lstMainTextureList.end())      {          if((*itTextures)->m_sFilename==sFilename)          //texture already loaded, so return          g_Log<<"LoadTexture("<<sFilename<<") ALREADY LOADED"<<endl;      }


hmm, unless I'm being stupid (possible!) that loop doesn't iterate - you never move the itTextures pointer forwards..? and it won't return when it finds an existing resource, just spit out a message.

Same thing again with:
//void CTextureManager::SetTexture(string sFilename)while(itTextures!=m_lstMainTextureList.end())	{		//found texture		if((*itTextures)->m_sFilename==sFilename)		{			//set active texture			g_pD3DDevice9->SetTexture(0,(*itTextures)->m_texture);			g_Log<<"SetTexture("<<sFilename<<") SUCCEEDED"<<endl;			return;		}	}

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:
hmm, unless I'm being stupid (possible!) that loop doesn't iterate - you never move the itTextures pointer forwards..? and it won't return when it finds an existing resource, just spit out a message.


Thanks for that. I kinda forgot to add the ++iterator bit, because I am so use to for loops, heh. I have fixed that problem now.

The main problem is still there though. The post below gives a more useful error message.

[Edited by - utilae on February 15, 2005 3:07:58 PM]

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

I have done the debug again:

First-chance exception at 0x004bc177 in RTS Project.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x004bc177 in RTS Project.exe: 0xC0000005: Access violation reading location 0x00000000.
The program '[1964] RTS Project.exe: Native' has exited with code 0 (0x0).

I have found that it stops at this line:
newTexture->m_texture->GetLevelDesc(0,&surfaceDesc);//stops at this line
//Load texture from filevoid CTextureManager::LoadTexture(string sFilename){		//iterate through texture list	list<LOADEDTEXTURE*>::iterator itTextures=m_lstMainTextureList.begin();		while(itTextures!=m_lstMainTextureList.end())	{		if((*itTextures)->m_sFilename==sFilename)		{			//texture already loaded, so return			g_Log<<"LoadTexture("<<sFilename<<") ALREADY LOADED"<<endl;			return;		}		else ++itTextures;	}	//texture is not already loaded, so now a texture can be loaded	D3DSURFACE_DESC surfaceDesc;	LOADEDTEXTURE *newTexture=new LOADEDTEXTURE;	//load texture	D3DXCreateTextureFromFileEx(g_pD3DDevice9,sFilename.c_str(),D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,0,0,&newTexture->m_texture);	newTexture->m_texture->GetLevelDesc(0,&surfaceDesc);//stops at this line	//store textures filename, height and width	newTexture->m_sFilename=sFilename;	newTexture->m_nHeight=surfaceDesc.Height;	newTexture->m_nWidth=surfaceDesc.Width;	//put texture into list	m_lstMainTextureList.push_back(newTexture);	//texture now loaded	g_Log<<"LoadTexture("<<sFilename<<") SUCCEEDED"<<endl;	return;}


For that line I also get this error:

An unhandled exception of type 'System.NullReferenceException' occured.
Object reference not set to an instance of an object.

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

This topic is closed to new replies.

Advertisement