title: Allocation of data through a list of pointers in C++
body: <p>I'm trying to read the data of several objects from a file. The reading itself works fine, but I'm having trouble with writing the data into a <code>std::list&lt;MyObject*&gt;</code>. I tried a couple of things but it always ends with the application crashing after printing some stuff, which I think is random memory content. Below you can find the current version. I also tried Iterators but that didn't work either.
  Here's what happens in the calling method:</p>
  
  <pre><code>    PAProject proj2 = PAProject();
      proj2.projectName = "myfirstOne";
  
      PaFigureLoader::loadFigures(&amp;proj2);
  
      std::list&lt;PAFigure*&gt;::iterator figIterator;
      for(figIterator = proj2.figures.begin();
              figIterator != (proj2.figures.end());
              figIterator++) {
          PAFigure* fig = *figIterator;
          if(fig-&gt;firstname.empty()) {
              std::cout &lt;&lt; "Nothing to see here\n";
              break;
          } else {
              std::cout &lt;&lt; fig-&gt;firstname &lt;&lt; "\n";
          }
      }
  </code></pre>
  
  <p>The list is <code>std::list&lt;PAFigure*&gt; figures</code></p>
  
  <p>Method causing all the trouble:</p>
  
  <pre><code>static void loadFigures(PAProject *project)
  {
          //open input stream
      fs::ifstream ifstream(filename);
      std::string input;
      ifstream &gt;&gt; input;
      PAFigure * fig;
  
      //keep a string for all the splitting:
      std::string result;
  
      //define regexs
      std::regex reg1 ("(&lt;firstname&gt;)([a-zA-Z0-9]*)(&lt;/firstname&gt;)");
      std::regex reg2 ("(&lt;lastname&gt;)([a-zA-Z0-9]*)(&lt;/lastname&gt;)");
  
      //iterate through file to find all figures
      while(input.compare("&lt;/allfigures&gt;") != 0) {
  
          //do the figure-loading stuff
          if(input.compare("&lt;figure&gt;")==0) {
              PAFigure newFigure = PAFigure();
              project-&gt;figures.push_front(&amp;newFigure);
              fig = &amp;newFigure;
          }
  
          //find contents
          if(input.find("&lt;firstname&gt;")!= std::string::npos) {
              std::regex_replace (std::back_inserter(result), input.begin(), input.end(), reg1, "$2");
              fig-&gt;firstname = result;
              result.erase();
              std::cout &lt;&lt; fig-&gt;firstname &lt;&lt; " is firstname\n";
          }
          if(input.find("&lt;lastname&gt;")!= std::string::npos) {
              std::regex_replace (std::back_inserter(result), input.begin(), input.end(), reg2, "$2");
              fig-&gt;lastname = result;
              result.erase();
          }
  
          //read next line
          ifstream &gt;&gt; input;
      }
      PAFigure * figtest = project-&gt;figures.back();
      std::cout &lt;&lt; figtest-&gt;firstname &lt;&lt; " last element\n";
  
      figtest = project-&gt;figures.front();
      std::cout &lt;&lt; figtest-&gt;firstname &lt;&lt; " first element\n";
  }
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre><code> died
  Anna is firstname
   died
  Dorian is firstname
  Dorian last element
  Dorian first element
  Dorian died
  Anna died
  </code></pre>
  
  <p>I added</p>
  
  <pre><code>PAFigure::~PAFigure()
  {
     std::cout &lt;&lt; this-&gt;firstname &lt;&lt; " died\n";
  
  } 
  </code></pre>
  
  <p>because I had that weird feeling that my elements were just gone, and apparently the <code>PAFigure newFigure = PAFigure()</code> does infact never get a firstname.</p>
  
  <p>I admit that my coding experience in C++ and especially with pointer/references is very... basic. I have no idea how to solve this, not even talking about solving it in an elegant way.</p>