Invalid access error when trying to access a pointer to an object in a vector
<p>I've a method in a class that get's a pointer to another object (of a different class)and adds an object (of another different class) to a vector that is a member variable of the first object (the one that is passed as a parameter). This is the code:</p>

<pre><code>ObstacleManager::ObstacleManager(Application *lApp)
{
    app=lApp;
    GLfloat obstacleVerts[12]={
    -0.1f,-0.2f,0.0f,
    0.1f,-0.2f,0.0f,
    -0.1f,0.2f,0.0f,
    0.1f,0.2f,0.0f
    };
    StandardObstacle obstacle(obstacleVerts,-0.7f,0.0f,4);
    obstacle.manager=this;
    lApp-&gt;characters.push_back(&amp;obstacle);
}
</code></pre>

<p>I think the problem is that the obstacle object gets released when it shouldn't, because if I change the code and create the obstacle with a "new" (if you create an object with new you have to manually delete it, don't you?) It works. Like this:</p>

<pre><code>ObstacleManager::ObstacleManager(Application *lApp)
{
    app=lApp;
    GLfloat obstacleVerts[12]={
    -0.1f,-0.2f,0.0f,
    0.1f,-0.2f,0.0f,
    -0.1f,0.2f,0.0f,
    0.1f,0.2f,0.0f
    };
    StandardObstacle *obstacle=new StandardObstacle(obstacleVerts,-0.7f,0.0f,4);
    obstacle-&gt;manager=this;
    lApp-&gt;characters.push_back(obstacle);
}
</code></pre>

<p>Is there a way to prevent this from happening?</p>
