glTexImage2D crashes

I need your help once more.

i tried to collect the data of all textures first before i give it to the shader.
Here is my latest commit: collect all texture data and give it to the shader later, (e5490b94) · Commits · C-Fu / OpenGL · GitLab

If there is no texture (so the catch block is executed) it works fine. But if there is a texture, it crashes. Anyone who sees the problem?? the old version where i gave the data to the shader once in the try and once in the catch block it worked.
I don’t see the different that causes the crash

==EDIT==

i figured out that this works:


try
		{
			if (it->texture == "")
				throw std::invalid_argument("no texture name");

			std::string tempPath = path;

			while (tempPath.back() != '/' && tempPath.back() != '\\')
				tempPath.pop_back();

			TextureTGA tempTex(std::string(tempPath + vModels.front()->texture).c_str());

			tempData->alpha = tempTex.hasAlpha();
			tempData->width = tempTex.getWidth();
			tempData->height = tempTex.getHeight();
			tempData->data = tempTex.getData().data();

			vTextures.push_back(tempData);

			glTexImage2D(GL_TEXTURE_2D,
				0,
				vTextures.front()->alpha ? GL_RGBA : GL_RGB,
				vTextures.front()->width,
				vTextures.front()->height,
				0,
				vTextures.front()->alpha ? GL_BGRA : GL_BGR,
				GL_UNSIGNED_BYTE,
				tempData->data = tempTex.getData().data()
			);
		}

But this does not


try
		{
			if (it->texture == "")
				throw std::invalid_argument("no texture name");

			std::string tempPath = path;

			while (tempPath.back() != '/' && tempPath.back() != '\\')
				tempPath.pop_back();

			TextureTGA tempTex(std::string(tempPath + vModels.front()->texture).c_str());

			tempData->alpha = tempTex.hasAlpha();
			tempData->width = tempTex.getWidth();
			tempData->height = tempTex.getHeight();
			tempData->data = tempTex.getData().data();

			vTextures.push_back(tempData);

			glTexImage2D(GL_TEXTURE_2D,
				0,
				vTextures.front()->alpha ? GL_RGBA : GL_RGB,
				vTextures.front()->width,
				vTextures.front()->height,
				0,
				vTextures.front()->alpha ? GL_BGRA : GL_BGR,
				GL_UNSIGNED_BYTE,
				vTextures.front()->data
			);
		}

===SOLVED===

Found the problem. The data wasn’t valid outside the try block

Any ideas why?? (in that last case the cube is solid grey) and if i do that after the try/catch block it crashes.

with only the try-part it is hard to tell, but most likely the destructor of TextureTGA is called, so tempTex frees all the allocations (data) it holds.
C++ is a rather hard language, a lot of stuff happens behind the scenes - I would recommend plain C when beginning.

I’m facing a similar issue. It seems that glTexImage2D() is not executed right away, but has some kind of a deferred processing like all OpenGL commands. So it may happen that tempTex gets out of scope and gets destroyed before the actual data are copied to the OpenGL texture. Then copying destroyed data causes the crash.

You can add glFinish() right after the glTexImage2D() to ensure that all issued OpenGL commands were executed.

That is not how OpenGL works, see section 2.1 “Execution Model” of the spec:

You are correct that implementations will defer executing commands, but they must still abide by the above requirement. In practice that usually means making a copy of the user supplied data before the OpenGL call returns and performing the deferred operations using that internal copy.

PS: Please don’t re-open ancient threads.