• Create Account

Banner advertising on our site currently available from just \$5!

### #ActualAshaman73

Posted 22 July 2013 - 07:18 AM

But for some reason, just this one entity "Blan" where I creates with "NewEntity()" it pop up a error saying that I can't access this memory when the program reach to the delete section of the code.

Your NewEntity method create the entity on the stack and not on the heap:

Entity testing;
testing.name = name;
TotalEntity.push_back(testing);


Once your NewEntity methods returns, the testing entity will be gone resulting most likely in a followup error once you try to access/delete it.

The correct way would be something like this:

Entity* testing = new Entity();
testing->name = name;
TotalEntity.push_back(*testing);

Update:

I know, that a vector::push_back creates a copy of the data, but this does not prevent you from using pointers as type of the vector. Can you give more information, like the definition of TotalEntity.

Update:

It seems that I'm on the wrong track...

### #5Ashaman73

Posted 22 July 2013 - 07:15 AM

But for some reason, just this one entity "Blan" where I creates with "NewEntity()" it pop up a error saying that I can't access this memory when the program reach to the delete section of the code.

Your NewEntity method create the entity on the stack and not on the heap:

Entity testing;
testing.name = name;
TotalEntity.push_back(testing);


Once your NewEntity methods returns, the testing entity will be gone resulting most likely in a followup error once you try to access/delete it.

The correct way would be something like this:

Entity* testing = new Entity();
testing->name = name;
TotalEntity.push_back(*testing);

Update:

I know, that a vector::push_back creates a copy of the data, but this does not prevent you from using pointers/refrences as type of the vector. Can you give more information, like the definition of TotalEntity (e.g. is it just vector<Entity> or maybe vector<Entity&> ? ).

### #4Ashaman73

Posted 22 July 2013 - 07:14 AM

But for some reason, just this one entity "Blan" where I creates with "NewEntity()" it pop up a error saying that I can't access this memory when the program reach to the delete section of the code.

Your NewEntity method create the entity on the stack and not on the heap:

Entity testing;
testing.name = name;
TotalEntity.push_back(testing);


Once your NewEntity methods returns, the testing entity will be gone resulting most likely in a followup error once you try to access/delete it.

The correct way would be something like this:

Entity* testing = new Entity();
testing->name = name;
TotalEntity.push_back(*testing);

Update:

I know, that a vector makes a copy of the data, but this does not prevent you from using pointers/refrences as type of the vector. Can you give more information, like the definition of TotalEntity (e.g. is it just vector<Entity> or maybe vector<Entity&> ? ).

### #3Ashaman73

Posted 22 July 2013 - 07:07 AM

But for some reason, just this one entity "Blan" where I creates with "NewEntity()" it pop up a error saying that I can't access this memory when the program reach to the delete section of the code.

Your NewEntity method create the entity on the stack and not on the heap:

Entity testing;
testing.name = name;
TotalEntity.push_back(testing);


Once your NewEntity methods returns, the testing entity will be gone resulting most likely in a followup error once you try to access/delete it.

The correct way would be something like this:

Entity* testing = new Entity();
testing->name = name;
TotalEntity.push_back(*testing);

Update: thought you already consider this part. Can you give more information, like the definition of TotalEntity. Have you copy constructor/operator defined for Entity.

### #2Ashaman73

Posted 22 July 2013 - 06:58 AM

But for some reason, just this one entity "Blan" where I creates with "NewEntity()" it pop up a error saying that I can't access this memory when the program reach to the delete section of the code.

Your NewEntity method create the entity on the stack and not on the heap:

Entity testing;
testing.name = name;
TotalEntity.push_back(testing);


Once your NewEntity methods returns, the testing entity will be gone resulting most likely in a followup error once you try to access/delete it.

The correct way would be something like this:

Entity* testing = new Entity();
testing->name = name;
TotalEntity.push_back(*testing);

### #1Ashaman73

Posted 22 July 2013 - 06:58 AM

But for some reason, just this one entity "Blan" where I creates with "NewEntity()" it pop up a error saying that I can't access this memory when the program reach to the delete section of the code.

Your NewEntity method create the entity on the stack and not on the heap:

Entity testing;
testing.name = name;
TotalEntity.push_back(testing);


Once your NewEntity methods returns, the testing entity will be gone resulting most likely in a followup error once you try to access/delete it.

The correct way would be something like this:

Entity* testing = new Entity();
testing->name = name;
TotalEntity.push_back(*testing);

PARTNERS