PDA

View Full Version : glTexImage2D crashes



3DPrgmer
11-08-2016, 07:47 AM
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: https://git.rwth-aachen.de/carstenf/OpenGL/commit/e5490b9451db11641c1d897023f1ca288aa92a3a

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.

flyingpengiun
11-08-2016, 10:35 AM
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.