Strange crash...

Hi!
I have the following code that crash, but when I put a std::cout..¡ Not crash !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef GECONTENTMANAGER_H
#define GECONTENTMANAGER_H

#include <string>
#include <typeinfo>
#include <map>
#include <sstream>
#include <iostream>

#include "../include/object.hpp"
#include "../include/testclass.h"


class GEContentManager
{
    public:
        GEContentManager();

        char* rootDirectory;

        // Funcion que carga cualquier tipo de archivo a partir de una ruta y un T.
        // Funcion que carga un archivo a partir del rootDirectory + filename. Por ejemplo 'C:\\Data\' + 'cube.obj'
        template <class T> T* Load(const char* filename)
        {
            // Buscamos el objeto, para ver si ya esta registrado
            std::map<const char*, object*>::iterator it = loadedObjects.find(filename);

            // Retorna el puntero del objeto si esta almacenado ya
            if(it!=loadedObjects.end())
            {
                std::cout<<"El objeto "<<it->first<<" ya ha sido cargado"<<std::endl;
                return (T*)it->second;
            }
            //Si no lo hay, carga una nueva

            // Obtenemos la ruta del archivo
            std::stringstream sstm;
            sstm << rootDirectory << filename;
            std::cout<<"Load file "<<sstm.str()<<std::endl;            

            // Creamos un nuevo objeto del tipo enviado
            T* t = new T;
            // Llenamos el objeto
            t->Load(sstm.str().c_str());


            /*From this point if I put std::cout does not fail*/



            // Lo añadimos al mapa de objetos cargados
            loadedObjects.insert(std::pair<const char*, object*>(filename,t));
            // retornamos la instancia con los datos cargados
            return t;
        }

    protected:

    private:

        // Mapa donde almacenamos punteros hacia los objetos cargados.
        //Asi evitamos cargar dos veces un mismo objeto
        std::map<const char*, object*> loadedObjects;
};

#endif // GECONTENTMANAGER_H
Last edited on
The problem is this:
std::map<const char*, object*> loadedObjects;

Do not use a pointer as a key of the map. find() will compare the pointer. And I bet that you pass temporary pointer like this t->Load(sstm.str().c_str());. That will certainly lead to a crash
Then, what can I use as key? a std::string?
Last edited on
I replaced the const char * to std :: string and still did not work. Then place the std :: cout and returned to work. After I recompile without any changes ... and the program crash again!. I'm going crazy!
Yes std :: string is always better

They reason for the crash is not in your function Load().
What about rootDirectory? Is it correctly initialized? Better change it to std :: string as well.

I'd guess that something bad happens within t->Load(sstm.str().c_str());
Yeah!
I change rootDirectory to std::string but the program crash again. I change t->Load(sstm.str()) to t->Load(path) where path is a std::string variable with pathfile and....it works!!

Thanks for your help!
One more thing: if T is GEContentManager. You might have an infinite recursion
It´s true! Thanks!
Topic archived. No new replies allowed.