Memory problem

Pages: 123
hello

i have made a game using DirectX, and it runs fine. Until it shuts down.
then do i get the error:


"Unhandled exception at 0x6af7ad54 (msvcp100d.dll) in NewStickFigWars.exe: 0xC0000005: Access violation writing location 0xfeeefeee."


i know that 0xfeeefeee is freed memory, and that would suggest that i am deleting memory twice, but I didn't call a deconstructor or something simular.


my game has lots of classes,and it has vectors filled with pointers to classes from a array.

i am using Microsoft Visual Studio 2010 pro.
Last edited on
Are you sure you haven't called any destructor? This looks like one of those issues I'm afraid, the ones that have you commenting out all of your delete commands and destructor calls one by one and recompiling or going through the code with a debugger. Sorry.
Are you saying that you're not releasing all the objects you created?

Andy

P.S. How are you dealing with the DirectX objects? Are you using some kind of smart pointer?
Last edited on
I did not call a deconstructor manually, i thought that they were called automatically. i will try the idea of Computergeek01.

P.S. i have the DirectX objects in my own objects, and a pointer to that object is in a vector
i have commented out all the deletion code in my deconstructors, but still do i get the same error, even with empty deconstructors
Not sure if this is relevant, but...

The reason I asked about smart pointers is that if their destructors are fired -- to release a COM object -- after CoUnititialize has been called, you can see this kind of problems.

I always make sure that all my COM objects are released before calling CoUnititialize.

Can you see the stack trace which led to the exception?

Andy
i don't know what smart pointers are, so i guess that i don't use them.

when i look in the call stack: the yellow arrow pointed to the upper line wich said:

>	msvcp100d.dll!std::_Container_base12::_Orphan_all()  Line 202 + 0x5 bytes	C++


i think it might have something to do with the vectors
I've only ever used DirectX via COM interfaces; not the newer managed API. If you're using IDirect3D9Ex*, etc you're probably using raw pointers.

Do you have the rest of the stack trace? Are there any other useful clues in it? (like the type of object stored in the container)

Andy
Last edited on
Do you delete the pointers in the vector once the corresponding object from the array has been deleted or reached the end of it's scope?
I did not delete anything,but something else might do that.

here is the whole stack trace:

 	NewStickFigWars.exe!std::vector<int,std::allocator<int> >::_Tidy()  Line 1304 + 0xb bytes C++
 	NewStickFigWars.exe!std::vector<int,std::allocator<int> >::~vector<int,std::allocator<int> >()  Line 706	C++
 	NewStickFigWars.exe!Model::~Model()  Line 194 + 0xb bytes	C++
 	NewStickFigWars.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow)  Line 935 + 0x18f bytes	C++
 	NewStickFigWars.exe!__tmainCRTStartup()  Line 547 + 0x2c bytes	C
 	NewStickFigWars.exe!WinMainCRTStartup()  Line 371	C


the next things were light grey:

 	kernel32.dll!75dc3677() 	
 	ntdll.dll!770a9f02() 	
 	ntdll.dll!770a9ed5() 	



"Model" is the name of a class in my game, that class stores pointers to DirectX10 objects, and it has a empty deconstructor. that is a class wich i have in a vector.


EDIT:

i have found that one of the pointers in the Model class is invalid. the pointer has the value 0x00000004.

this only happened to one of the pointers in the Model class.(a pointer to a vertex buffer), however, all the Model classes in the vector do have this problem, and they have that problem the whole program, but does my program work

I don't know if this is relevant, but my program uses multiple threads
Last edited on
Your stack trace says that an instance of the Model is being destroyed during a call to WinMain. Or possibly as the call returns. Is this class a global?

In an earlier post you said you were not calling delete. DId I understand correctly?

Also, are you using #import to include the Direct3D or #include?

Andy


Last edited on
i did not call delete or something similar, you understand it correctly
i am using #include to include Direct3D.
Is the model object local to WinMain?

The stack trace is saying that it is being destroyed in WinMain.

Andy

P.S. You know how to set a memory breakpoint, yes?
Last edited on
so far as i know, there is only one type of break point, and i know how to set those.
It has a global scope.
You can set a breakpoint which is triggered when the data stored at a memory address changes.

If you you which variable has been prematurely deleted and set to 0xfeeefeee, you could set a data breakpoint on it to determine where it's first deleted.

MSDN describes this in:
"How Can I Find Out If My Pointers Corrupt a Memory Address?"
http://msdn.microsoft.com/en-US/library/w500y392%28v=VS.90%29.aspx

(This it the link to the VS2008 page; follows links to other versions of this page).
Last edited on
You could use the same technique (data breakpoint) to check out the pointer with value 4.

Regarding threads: are they also using the model directly? Do you stop the thread to quit before you exit WinMain?

And: I do think it would better if you could get your global object to dismantle itself before WinMain is exited. I've just had a look at the Windows SDK's Direct3D sample, and they are following the same approach (it is using DirectX 9, though). They call their Cleanup() function, to release all the interfaces, just before WinMain exits.

1
2
3
4
5
6
7
8
9
10
11
12
...

exit:
    if(g_hBackgroundThread)
    {
        KillBackgroundThread();
        WaitForSingleObject(g_hBackgroundThread,INFINITE);
    }

    Cleanup();
    return (int) msg.wParam;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
//--------------------------------------------------------------------------------------
// Clean up the objects we've created
//--------------------------------------------------------------------------------------
void Cleanup()
{
    SAFE_RELEASE(g_pD3D9);
    SAFE_RELEASE(g_pDevRealTime);
    SAFE_RELEASE(g_pCursorVB);
    SAFE_RELEASE(g_pScreenVB);	
    SAFE_RELEASE(g_pSharedBackgroundTexture);
    SAFE_RELEASE(g_pFont);
    SAFE_RELEASE(g_pSprite);
} 


where

#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p) = NULL; } }
Last edited on
i have used the data breakpoints, and i think the error is in a member of the model class, wich is a vector with integers. i had put a data breakpoint at the adres of a member vector(the vector with the integers) of the last model in the vector, and direct after that breakpoint was hit, i got the error.

i guess that the integers-vector member is double destroyed. So, how do i stop this?

the second thread of my program(that thread is created by a multimedia timer) stops before WinMain ends. The models are only used directly in the beginning of the program, after that, the program uses pointers to them. The pointers to the models are stored in a vector.


the deconstructors used to have that code that you have in the Cleanup() function, but i removed that because i thought it was removed twice.

but i will give it a try to clean everything up before WinMain ends.
You should be able to add some sort of logic to fix the problem. How is the in vector declared?
the Model class is declared like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Model
{
private:
	bool			bIsCleaned;// the variable wich prevents double deconstruction in the deconstructors 
public: 
	bool			bIsBullet;

	vector<Vertex>				vRawData;
	vector<int>					vRawIndexData;
	ID3D10ShaderResourceView*	m_pTexture;
	D3D10_PRIMITIVE_TOPOLOGY	m_Topology;
	ID3D10Buffer*				m_pVertexBuffer;
	ID3D10Buffer*				m_pIndexBuffer;
	UINT						m_uiNumberOfIndecis;

	float						m_fXMax;
	float						m_fXMin;

	float						m_fYMax;
	float						m_fYMin;

	float						m_fZMax;
	float						m_fZMin;


	


	HRESULT LoadTexture(wstring Texture,ID3D10Device* pDevice);
	HRESULT LoadModelFromDotModel(wstring File,D3DXVECTOR4 Color, ID3D10Device* pDevice);
	HRESULT LoadModelFromWaveFront(string File,D3DXVECTOR4 Color,ID3D10Device* pDevice);
	
	Model();
	~Model();

	
	
	void UnloadModel();

	D3DXMATRIX			m_TransformMatrix;
}


and how do you mean "some sort of logic"?
Well, your original post was about an access violation trying to write to feeefeee, so one possibility was to stop that write if possible (but I don't/didn't have enough info to know if that's possible, hence question about the call stack, étc)

And you were worried about double destroying the vector, so there was a tiny chance that it it had been newed. Then there might have been in the pointer guard logic. But as it is just a regular vector, that's probably irrelevant.

As it stands, the only way the int vector destructor would be called twice is if the model was destroyed twice. So it looks like it's probably a memory trampling issues.

Can you see what's setting the pointer value to 4 ???
Pages: 123