IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

Crash à l'appel de la fonction insert de std::string.


Sujet :

Langage C++

  1. #1
    Invité
    Invité(e)
    Par défaut Crash à l'appel de la fonction insert de std::string.
    Salut! J'essaie d'appeler la fonction insert de la classe std::string pour insérer du texte dans un fichier cpp.
    Voici le contenu du fichier cpp et donc de ma std::string.
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include "/home/laurent/Développement/Projets-c++/ODFAEGCREATOR/test/test.hpp"
    using namespace odfaeg::graphic;
    test::test(sf::VideoMode vm, std::string title) : 
    Application (vm, title, sf::Style::Resize|sf::Style::Close, sf::ContextSettings(0, 0, 0, 3, 0)) {
    }
    void test::onLoad() {
    }
    void test::onInit() {
    }
    void test::onRender(RenderComponentManager *cm) {
    }
    void test::onDisplay(RenderWindow* window) {
       if (&getRenderWindow() == window) {
           for (unsigned int i = 0; i < drawables.size(); i++) {
               window->draw(*drawable[i]);
           }
       }
    }
    void test::onUpdate (RenderWindow* window, sf::Event& event) {
     if (&getRenderWindow() == window && event.type == sf::Event::Closed) {
      stop();
     }
    }
    void test::onExec () {
    }
    Voici l'endroit ou ça crash :
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    if (item->getText() == "Rectangle shape") {
            std::unique_ptr<RectangleShape> shape = std::make_unique<RectangleShape>(Vec3f(100, 50, 0));
            shape->setPosition(cursor.getPosition());
            displayInfos(shape.get());
            selectedObject = shape.get();
            shapes.push_back(std::move(shape));
            unsigned int pos = cppAppliContent.find("onInit() {");
            pos += 11;
            cppAppliContent.insert(pos,"std::unique_ptr<sf::RectangleShape> shape"+conversionIntString(shape->getId())+" = std::make_unique<RectangleShape>(Vec3f(100, 50, 0));\n"
                                       "drawables.push_back(std::move(shape));\n");
            std::cout<<"file content : "<<cppAppliContent<<std::endl;
        }

    std::find me trouve bien la position du premier caractère, j'ajoute 11 car je veux insérer le texte à la fin de la chaîne recherchée.
    mais à l'appel de la fonction insert, ça crash, voici ce que m'affiche le débugeur.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    #0 0x8059c57	ODFAEGCreator::actionPerformed(this=0xbffef558, item=0x838bc38) (/home/laurent/Développement/Projets-c++/ODFAEGCREATOR/application.cpp:479)
    #1 0x8100a70	odfaeg::core::DynamicWrapper<void, odfaeg::graphic::gui::MenuItemListener, odfaeg::graphic::gui::MenuItem*>::operator()<odfaeg::graphic::gui::MenuItemListener<odfaeg::graphic::gui::MenuItem*&> >(this=0x8388098, o=0xbffef5b0) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/GUI/../../../../include/odfaeg/Graphics/GUI/../../Math/../Core/fastDelegate.h:309)
    #2 0x810041c	std::_Function_handler<void (odfaeg::graphic::gui::MenuItemListener*&, odfaeg::graphic::gui::MenuItem*&), odfaeg::core::DynamicWrapper<void, odfaeg::graphic::gui::MenuItemListener<odfaeg::graphic::gui::MenuItem*> > >::_M_invoke(std::_Any_data const&, odfaeg::graphic::gui::MenuItemListener*&, odfaeg::graphic::gui::MenuItem*&)(__functor=..., __args#0=@0x836509c: 0xbffef5b0, __args#1=@0x838808c: 0x838bc38) (/usr/include/c++/4.9/functional:2039)
    #3 0x810202c	std::function<void (odfaeg::graphic::gui::MenuItemListener*&, odfaeg::graphic::gui::MenuItem*&)>::operator()(odfaeg::graphic::gui::MenuItemListener*&, odfaeg::graphic::gui::MenuItem*&) const(this=0x8b5efa4, __args#0=@0x836509c: 0xbffef5b0, __args#1=@0x838808c: 0x838bc38) (/usr/include/c++/4.9/functional:2440)
    #4 0x810150d	odfaeg::core::FastDelegateImpl<void, odfaeg::graphic::gui::MenuItemListener*, odfaeg::graphic::gui::MenuItem*>::call<0u, 1u, void*&, void*&, void*&, void*&>(this=0x8b5efa0) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/GUI/../../../../include/odfaeg/Graphics/GUI/../../Math/../Core/fastDelegate.h:480)
    #5 0x8100e12	odfaeg::core::FastDelegateImpl<void, odfaeg::graphic::gui::MenuItemListener*, odfaeg::graphic::gui::MenuItem*>::operator() (this=0x8b5efa0, p0=0x0, p1=0x0, p2=0x0, p3=0x0) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/GUI/../../../../include/odfaeg/Graphics/GUI/../../Math/../Core/fastDelegate.h:449)
    #6 0x8080a22	odfaeg::core::FastDelegate<void>::operator()<>() const(this=0x8b5ee00) (/usr/local/include/odfaeg/Core/fastDelegate.h:552)
    #7 0x8142601	odfaeg::core::Command::operator() (this=0x8b5edfc) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Core/command.cpp:89)
    #8 0x8096a9a	odfaeg::core::Listener::processEvents(this=0x838bde4) (/usr/local/include/odfaeg/Core/listener.h:127)
    #9 0x80e98f1	odfaeg::graphic::LightComponent::processEvents(this=0x838bc38) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/GUI/../../../../include/odfaeg/Graphics/GUI/../lightComponent.h:107)
    #10 0x80cf8b0	odfaeg::graphic::RenderComponentManager::updateComponents(this=0x82ae278) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/renderComponentManager.cpp:116)
    #11 0x8097047	odfaeg::core::Application::render(this=0xbffef558) (/usr/local/include/odfaeg/Core/application.h:130)
    #12 0x8096db2	odfaeg::core::Application::exec(this=0xbffef558) (/usr/local/include/odfaeg/Core/application.h:82)
    #13 0x8095ec6	main(argc=1, argv=0xbffff644) (/home/laurent/Développement/Projets-c++/ODFAEGCREATOR/main.cpp:10)
    Bref, je ne comprend pas pourquoi ça crash, théoriquement il devrait m'insérer la chaîne de caractère sans problème.

  2. #2
    Invité
    Invité(e)
    Par défaut
    J'ai essayé de code une fonction insert moi même et il crash dès l'appel à la fonction, il ne rentre même pas dans la fonction.

  3. #3
    Invité
    Invité(e)
    Par défaut
    Ha, c'est ma fonction conversionIntString qui plante.

  4. #4
    Invité
    Invité(e)
    Par défaut
    shape->getId() renvoie un const unsigned int&

    Lorsque je le passe ici :
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    std::string str = conversionUIntString(shape->getId());

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    std::string conversionUIntString(Uint32 ui) {
                std::stringstream ss;
                ss<<ui;
                return ss.str();
            }

    Ca plante.

    Ne puis je pas passer un const unsigned int& et la transformer en std::string ? Ou bien est ce le fait que j'envoie une référence ?

  5. #5
    Invité
    Invité(e)
    Par défaut
    Salut j'ai changé la fonction :
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    std::string conversionUIntString(Uint32& ui) {
          std::stringstream ss;
          ss<<ui;
          return ss.str();
    }
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    conversionUIntString(const_cast<unsigned int&>(shape->getId()))

    Et j'ai cette erreur là :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    #0 0x8144d16	odfaeg::core::conversionUIntString(ui=<error reading variable>) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Core/utilities.cpp:49)
    #1 0x805a102	ODFAEGCreator::actionPerformed(this=0xbffef558, item=0x8391738) (/home/laurent/Développement/Projets-c++/ODFAEGCREATOR/application.cpp:490)
    #2 0x8104636	odfaeg::core::DynamicWrapper<void, odfaeg::graphic::gui::MenuItemListener, odfaeg::graphic::gui::MenuItem*>::operator()<odfaeg::graphic::gui::MenuItemListener<odfaeg::graphic::gui::MenuItem*&> >(this=0x89d05b8, o=0xbffef5b0) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/GUI/../../../../include/odfaeg/Graphics/GUI/../../Math/../Core/fastDelegate.h:309)
    #3 0x8103fe2	std::_Function_handler<void (odfaeg::graphic::gui::MenuItemListener*&, odfaeg::graphic::gui::MenuItem*&), odfaeg::core::DynamicWrapper<void, odfaeg::graphic::gui::MenuItemListener<odfaeg::graphic::gui::MenuItem*> > >::_M_invoke(std::_Any_data const&, odfaeg::graphic::gui::MenuItemListener*&, odfaeg::graphic::gui::MenuItem*&)(__functor=..., __args#0=@0x838e4f4: 0xbffef5b0, __args#1=@0x838e504: 0x8391738) (/usr/include/c++/4.9/functional:2039)
    #4 0x8105bf2	std::function<void (odfaeg::graphic::gui::MenuItemListener*&, odfaeg::graphic::gui::MenuItem*&)>::operator()(odfaeg::graphic::gui::MenuItemListener*&, odfaeg::graphic::gui::MenuItem*&) const(this=0x8b679ec, __args#0=@0x838e4f4: 0xbffef5b0, __args#1=@0x838e504: 0x8391738) (/usr/include/c++/4.9/functional:2440)
    #5 0x81050d3	odfaeg::core::FastDelegateImpl<void, odfaeg::graphic::gui::MenuItemListener*, odfaeg::graphic::gui::MenuItem*>::call<0u, 1u, void*&, void*&, void*&, void*&>(this=0x8b679e8) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/GUI/../../../../include/odfaeg/Graphics/GUI/../../Math/../Core/fastDelegate.h:480)
    #6 0x81049d8	odfaeg::core::FastDelegateImpl<void, odfaeg::graphic::gui::MenuItemListener*, odfaeg::graphic::gui::MenuItem*>::operator() (this=0x8b679e8, p0=0x0, p1=0x0, p2=0x0, p3=0x0) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/GUI/../../../../include/odfaeg/Graphics/GUI/../../Math/../Core/fastDelegate.h:449)
    #7 0x8080f96	odfaeg::core::FastDelegate<void>::operator()<>() const(this=0x8b4f310) (/usr/local/include/odfaeg/Core/fastDelegate.h:552)
    #8 0x81462bb	odfaeg::core::Command::operator() (this=0x8b4f30c) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Core/command.cpp:89)
    #9 0x809751c	odfaeg::core::Listener::processEvents(this=0x83918e4) (/usr/local/include/odfaeg/Core/listener.h:127)
    #10 0x80ed4b7	odfaeg::graphic::LightComponent::processEvents(this=0x8391738) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/GUI/../../../../include/odfaeg/Graphics/GUI/../lightComponent.h:107)
    #11 0x80d3e44	odfaeg::graphic::RenderComponentManager::updateComponents(this=0x82b27b8) (/home/laurent/Développement/Projets-c++/ODFAEG/src/odfaeg/Graphics/renderComponentManager.cpp:116)
    #12 0x8097ac9	odfaeg::core::Application::render(this=0xbffef558) (/usr/local/include/odfaeg/Core/application.h:130)
    #13 0x8097834	odfaeg::core::Application::exec(this=0xbffef558) (/usr/local/include/odfaeg/Core/application.h:82)
    #14 0x809643a	main(argc=1, argv=0xbffff644) (/home/laurent/Développement/Projets-c++/ODFAEGCREATOR/main.cpp:10)
    Ca veut dire quoi error reading variable ?

    Et pourquoi il m'affiche ça ?

  6. #6
    Expert éminent sénior

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 189
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 189
    Points : 17 141
    Points
    17 141
    Par défaut
    le "error reading variable", c'est tout simplement que ton debuggueur (gdb?) ne sait pas afficher un "Uint32&"

    Cela dit pourquoi ta fonction prends une référence non constante, alors même qu'elle ne doit clairement pas modifier son argument?
    Tu devrais garder en tête que const_cast et reinterpret_cast sont toujours des erreurs. A moins d'avoir une excellente raison, justifiée comme il se doit.

    La réponse, c'est que ta fonction ne getId() ne retourne pas le type que tu crois.

    Pourquoi ne pas utiliser std::to_string(x) cf reference
    Mes principes de bases du codeur qui veut pouvoir dormir:
    • Une variable de moins est une source d'erreur en moins.
    • Un pointeur de moins est une montagne d'erreurs en moins.
    • Un copier-coller, ça doit se justifier... Deux, c'est un de trop.
    • jamais signifie "sauf si j'ai passé trois jours à prouver que je peux".
    • La plus sotte des questions est celle qu'on ne pose pas.
    Pour faire des graphes, essayez yEd.
    le ter nel est le titre porté par un de mes personnages de jeu de rôle

  7. #7
    Invité
    Invité(e)
    Par défaut
    J'avais enlevé le const car je croyais que l'erreur venait du fait que ss n'arrivait pas à modifier la variable du fait qu'elle était const mais ça n'était pas ça.
    Je vais essayé tostring mais pourtant Uint32& et id sont de type unsigned int mais même lorsque j'essaye de mettre mon unsigned int& dans un unsigned int ça plante je dois obligatoirement utiliser une référence dans ma fonction de conversion sinon ça plante.

  8. #8
    Invité
    Invité(e)
    Par défaut
    J'ai trouvé pourquoi ça plante, c'est une bête erreur de ma part, en fait std::move copie le contenu de mon std::unique_ptr dans un autre std::unique_ptr et supprime le contenu du std::unique_ptr copié il fallait donc que j'insère mon unique_ptr dans mon std::vector après l'appel de la fonction de conversion.
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    std::unique_ptr<RectangleShape> shape = std::make_unique<RectangleShape>(Vec3f(100, 50, 0));
            shape->setPosition(cursor.getPosition());
            displayInfos(shape.get());
            selectedObject = shape.get();
            unsigned int pos = cppAppliContent.find("onInit() {");
            pos += 11;
            std::string toInsert = "    std::unique_ptr<sf::RectangleShape> shape"+conversionUIntString(shape->getId())+" = std::make_unique<RectangleShape>(Vec3f(100, 50, 0));\n"
                                  "    drawables.push_back(std::move(shape));\n";
            cppAppliContent.insert(pos, toInsert);
            std::cout<<"file content : "<<cppAppliContent<<std::endl;
            shapes.push_back(std::move(shape));

    Moi je l'avais mis ici donc ça n'allait pas.
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    std::unique_ptr<RectangleShape> shape = std::make_unique<RectangleShape>(Vec3f(100, 50, 0));
            shape->setPosition(cursor.getPosition());
            displayInfos(shape.get());
            selectedObject = shape.get();
            shapes.push_back(std::move(shape));
            unsigned int pos = cppAppliContent.find("onInit() {");
            pos += 11;
            std::string toInsert = "    std::unique_ptr<sf::RectangleShape> shape"+conversionUIntString(shape->getId())+" = std::make_unique<RectangleShape>(Vec3f(100, 50, 0));\n"
                                  "    drawables.push_back(std::move(shape));\n";
            cppAppliContent.insert(pos, toInsert);
            std::cout<<"file content : "<<cppAppliContent<<std::endl;

    Parfois on cherche pendant des heures hors que l'erreur est toute bête putain!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Crash lors de l'appel d'une fonction membre :)
    Par Invité dans le forum C++
    Réponses: 2
    Dernier message: 06/01/2019, 01h35
  2. [Python 2.X] Cx_freeze : Crash du .exe à cause d'appels récursifs de fonctions
    Par Triton972 dans le forum Déploiement/Installation
    Réponses: 12
    Dernier message: 09/02/2015, 23h24
  3. Réponses: 2
    Dernier message: 14/06/2010, 15h21
  4. A la recherche de l'appel d'une fonction...
    Par karl3i dans le forum C
    Réponses: 3
    Dernier message: 24/09/2003, 12h34
  5. appel xmlservice via fonction javascript
    Par pram dans le forum XMLRAD
    Réponses: 2
    Dernier message: 06/05/2003, 14h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo