Release() Problem

Started by
5 comments, last by andyb716 19 years, 6 months ago
Hi I'm trying to make a dynamic texture loader in my graphics class here is my function.

BOOL cGraphics::CreateTexture(char *File){

	//New Texture
	cTexture texture;

	texture.Load(m_pD3DDevice, File);

	//Create iterator
	std::vector<cTexture>::iterator it = m_Texture.end();

	m_Texture.insert(it, texture);

	texture.Free();

	return TRUE;
}

m_Graphics.CreateTexture("LandWater.bmp");
m_Graphics.CreateTexture("Water.bmp");
m_Graphics.CreateTexture("LandWater2.bmp");
m_Graphics.CreateTexture("airplane.tga");

I can load one or two textures but then it gives me an could not find memory referenced at wherever error, meaning that my IDirect3D9Texture is not null but it hasnt been set right? My question is am I doing this right, is there another way to do this. Do I have to use the NEW operator in creating a new Texture to insert into my vector. Is my vector not bieng set right? By the way I'm not sure if this is a DirectX Problem or just a C++ problem. And Im totally new at this so any help would be appreciated thanks.
Advertisement
I don't use STL containers, but I doubt it will automatically copy dynamically allocated memory from a texture. texture.Load() is allocating memory to store a texture. m_Texture.insert() is copying the texture structure (I'm guessing here), but not the allocated memory block from load(). texture.Free() is destroying the block of allocated memory from Load(), which is the only version of that memory in your program.

Don't use free() in this function. Only free the textures when you are done with them and want to remove them from the list. And if your textures' destructor also calls free, make sure to add the ability to null out a texture's internal reference to the real allocated texture when it is copied. [smile]

Let me know if I skipped over an important detail.
I took out the free function and did a debug on the Load part. It seems to load the first texture and then calls the desctructor where I have the free() function. What else would I use besides STL in order to use dynamic arrays?
Sorry, I edited my post without adding an EDIT tag [smile]. See the bottom part again. You'll have to null out the internal pointer in cTexture.

EDIT: STL is just a wrapper class. You aren't in the wrong for using it. Most people do. I'm just not most people.
And if your textures' destructor also calls free, make sure to add the ability to null out a texture's internal reference to the real allocated texture when it is copied.

I dont understand this. Isnt this what I am doing when I call the Free function?
I'm guessing that free calls the API texture->Release(), right? This will delete the memory that was created in Load(). And your cTexture class is calling Free() in it's destructor? You probably have something like the following in Free():

if(Internal_texture)
{
Internal_texture->Release();
Internal_texture = NULL;
}

If not, make it simular to that. Now right after you insert the texture into the list, call a function something like this:

cTexture::Nullify()
{
Internal_texture = NULL;
}

I'm pretty sure STL is making an exact copy of your texture class structure. But pointers are just addresses, so a copy is not made of the memory it is pointing to. So you allocate it once, make a copy of the structure, then keep the allocation in the copy, and nullify the original. If you call Release() on the original, you delete the memory that both copies of the texture class are pointing to.
thanks

This topic is closed to new replies.

Advertisement