Witam,
Napisałem sobie kodzik do wczytywania danych z plików obj.
Użyłem wektorów. Niestety używanie mojej klasy która tworzyła wiele kilka wektorów powoduje w dalszej części programu memory corruption.
Zdaję sobie sprawe z możliwości użycia funkcji do kilku fragmentów, tak było - zaniechałem tej idei celem sprawdzenia czy zmiana kontekstu ma jakieś znaczenie.
Zmienne file oraz filename to pola klasy (zainicjalizowane, dane wczytywane są poprawnie do this->vertices, this->normals.
Błąd nadal występuje - program przejdzie przez ten fragment kodu, lecz w dalszym etapie wykonywania zupełnie innego kodu nastąpi memory corruption(w zupełnie innym juz kontekście).
Bez względu na to czy na końcu metody process wywołam delete dla każdego wskaźnika dla wektora czy nie - problem występuje.
void ObjReader::process()
{
verticesAmount = 0;
texturesAmount = 0;
normalsAmount = 0;
vector<float> *vertices = new vector<float>();
vector<float> *textures = new vector<float>();
vector<float> *normals = new vector<float>();
vector<int> *verticesIndexes = new vector<int>();
vector<int> *texturesIndexes = new vector<int>();
vector<int> *normalsIndexes = new vector<int>();
file.open(filename);
string tempString;
while (!file.eof())
{
file >> tempString;
if(tempString == "v")
{
ObjReader::readThreeAndPushEm(vertices);
}
else if(tempString == "vt")
{
ObjReader::readThreeAndPushEm(textures);
}
else if(tempString == "vn")
{
ObjReader::readThreeAndPushEm(normals);
}
else if(tempString == "f")
{
for (int i = 0; i < 3; i++)
{
file>>tempString;
replaceEmptyTextureIndexToZero(tempString);
char *temp;
char *tempString2 = const_cast<char*>(tempString.c_str());
for (int j = 0; j < 3; ++j)
{
j==0 ? temp = strtok(tempString2, "/")
: temp = strtok(nullptr, "/");
int index = static_cast<int>(strtol(temp, nullptr, 10) - 1);
if(index != -1)
{
switch (j)
{
case 0:
verticesIndexes->push_back(index);
break;
case 1:
texturesIndexes->push_back(index);
break;
case 2:
normalsIndexes->push_back(index);
break;
default:
break;
}
}
}
}
}
else
{
std :: cout << "Ignored obj data from file " << filename << " :" << tempString << "\n";
}
}
this->vertices = new float[verticesIndexes->size()*4];
this->normals = new float[normalsIndexes->size()*4];
int iterator = 0;
for (int k = 0; k < verticesIndexes->size(); ++k)
{
for (int i = 0; i < 4; ++i)
{
this->vertices[iterator++] = (*vertices)[(*verticesIndexes)[k] + i];
}
this->vertices[iterator++] = 1.0f;
}
iterator = 0;
for (int k = 0; k < normalsIndexes->size(); ++k)
{
for (int i = 0; i < 4; ++i)
{
this->normals[iterator++] = (*normals)[(*normalsIndexes)[k] + i];
}
this->normals[iterator++] = 0.0f;
}
}
void ObjReader::readThreeAndPushEm(vector<float> *vectorToPushTo)
{
float tempFloat;
file >> tempFloat;
vectorToPushTo->push_back(tempFloat);
file >> tempFloat;
vectorToPushTo->push_back(tempFloat);
file >> tempFloat;
vectorToPushTo->push_back(tempFloat);
}
void ObjReader::replaceEmptyTextureIndexToZero(string &basic_string)
{
for (int i = 1; i < basic_string.size(); ++i)
{
if(basic_string[i-1] == '/' && basic_string[i] == '/')
{
basic_string.insert(static_cast<unsigned long>(i), "0");
break;
}
}
}