Linked list Pains....

Started by
36 comments, last by JSCFaith 22 years, 5 months ago
I keep getting an error that says:

Error: Assertion Failuer.
File: dbgheap.c
Line: 1044

One question. Right now I have a LPDIRECTDRAWSURFACE7 which is a pointer to a surface of course. As I said, if I take that member out of the class it does not crash anymore. When the class ends and it deletes that memeber. Does it try to delete the surface as well? THe reason I ask is becuase right now, I have a resource manager thing. When the class runs it constructor, it calls a function that I created called RequestSurface(); This function requires the filename that you want to load into the surface and surface information. It creates the surface and then returns the pointer to it. So what I am trying to say is. If the class ends, and all its memebers are deleted, will it try to delete the surface that m_Surface (surface memeber in the class) is pointing too?
Advertisement
Well, I found the problem. It was crashing becuase I had a DWORD as one of the members of my CObject class. If I remove that everything works. No memeory leaks, all nodes are deleted.

Thanks for you help guys. I will start learning STL as soon as possible. When I finish this game I am working on, I will make sure to post it. Give me a week or two. As you can see, my one weak area is debugging. Well thanks again.

BTW stoffel, thanks for the debugging thing. I never knew about how to get memeory addresses and put them in a string. ("Created Explosion: 0x%08X\n", this). I didn''t know I could get memory addresses from pointers.

Later.
%x defaults to 8characters (on MSVC at least)

quote:
It was crashing becuase I had a DWORD as one of the members of my CObject class

I find that hard to believe, it may have just removed a little bit of padding that was keeping the memory checking RAM trail markers valid - meaning it's so messed up now the debugger doesn't even realize it.

quote:
Right now I have a LPDIRECTDRAWSURFACE7 which is a pointer to a surface of course. As I said, if I take that member out of the class it does not crash anymore. When the class ends and it deletes that memeber.

No! Bad programmer! Don't delete it!!!! If you call delete on it, this is likely the root of your troubles. Call ->Release() on it, and set it to zero. Or better yet, use the ATL smart pointer, CComPtr, and it will release stuff for you.

quote:
If the class ends, and all its memebers are deleted, will it try to delete the surface that m_Surface (surface memeber in the class) is pointing too?

Only if you call release (no delete interfaces!) on it in the dtor. Which IS something you want to do.


I don;t have the docs for Dx7 anymore, so here's a Dx8 example. The principles are the same.
    #include <ATLBase.h>class CGfxEng	{	protected:		CComPtr<IDirecct3D8> m_spD3D8;		CComPtr<IDirect3DSurface8> m_spPrimarySurface;		CComPtr<IDirect3DSurface8> m_spSecondarySurface;	};CGfxEng::Init(...)	{	m_spD3D8 = Direct3DCreate8(D3D_SDK_VERSION);	m_spD3D8->Release(); //D3DCreate8 return the pointer with a ref. count of 1, and the assign above increases it to 2.	//We want it to be 1.		//When you take the address of (&) a CComPtr it does not increase the reference count.	//So the reference count will be one when it comes out of here	m_spD3D8->CreateImageSurface(..., &m_spPrimarySurface);	}    

When CGfxEng is destroyed, the CComPtr's will automatically call release thier interface pointers.

Magmai Kai Holmlor
- Not For Rent

Edited by - Magmai Kai Holmlor on October 25, 2001 12:14:40 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
I agree with you Magmai Kai, a good map class is hard to find, the STL makes maps EASY! Need a symbol table for your script interpreter engine? Fire up a map template...

By the way !!!!! Call the cops, there''s a Direct Draw surface on the loose in JSC''s computer, since he didn''t call Release on it!!!! ))))))))))


(COM is such a pain in the ass...thank goodness for the ATL..)

-nt20


"nikolatesla20"
"nikolatesla20"
You guys obvously mis-interpreted what I was saying. The problem is this. I do release the surface.

m_Surface->Release(); // Just not in the CObject class

The only problem is this. When I create a object. It creates a surface in a class called CResources. Then CResources returns a pointer to the surface (LPDIRECTDRAWSURFACE7). when I have 4 or Lets say 6 asteroids that use the same surface. I don''t want to create the surface 6 times, thus, I have all six of the asteroids point to the same surface. Now if one asteroid is deleted and I release its surface, the surface it is pointing at is deleted as well. This not what I want to happen. Right now, I have more than just the problem above. That is just the base problem. If I release the surface, the other asteroids are pointing to nothing now. What should I do?

So this is what I do:

  // Called each time a object is deletedCObject::~CObject() {      m_Surface = NULL; // I can''t release the surface.};// Called at the end of the program.CResources::~CResources() {      ReleaseAllSurfaces();};// Now this releases all the CSurface objects I created during operation.CResources::ReleaseAllSurfaces() {CSurfaceNode *TempNode = CSurfaceNode::pFirst;CSurfaceNode *Node = CSurfaceNode::pFirst;while(TempNode != NULL) {           TempNode = TempNode->m_pNext;     delete Node;     Node = TempNode;};}// This is the destructor for the CSurface classCSurface::~CSurface() {     m_Surface->Release(); // Release the contents of the surface     m_Surface = NULL; // Not neccasary, but I do it anyways};  


Now I belive the code above should work. Well it doesn''t. My understand of LPDIRECTDRAWSURFACE7 objects is that they are just pointers to surfaces and not the actuall surface itself. Am I correct? Tell me if not.




When ever your return an interface pointer, call ->AddRef on it.
This increases the reference count, so a subsequent call to Release won''t delete it until the reference count reaches 0.
This is exactly what you want to do.

So leave CSurface''s dtor alone, but stick in a pD3D7Surface->Addref(); into the resource manager function that gets the surface pointer.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Oh, I have never ever seen that before. Thanks for the help Magmai Kai Holmlor. I will make sure to change that.

Thanks.

Edited by - JSCFaith on October 26, 2001 5:52:47 PM
Just an addition to what someone else has said...

at the point where you delete the node, you might try to make it like this

  if(node){  delte node;  node=NULL;}  


This way you will make completely sure you are not trying to delete the same object more than once. I don''t think delete explicitly sets the thing it deletes equal to NULL, but I could be wrong. Either way, I think it''s good practice to set deleted pointers to NULL heh.

Anthracks

This topic is closed to new replies.

Advertisement