473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Delete object that is stored in Vector

Hello all,

I have a question about deleting objects(the objects are stored in a vector)

I Fill the vector as following:
Expand|Select|Wrap|Line Numbers
  1.         servObj.push_back(new CGame(serversh, game_id)); (1)
  2.  
Since I am calling new, I need to call delete somewhere else in the program.

If I try to delete the first object that was stored in servObj like this:
Expand|Select|Wrap|Line Numbers
  1.                 delete &(servObj.at(0));
  2.  
And try to erase the first element after that I get segmentation fault(makes sense).

Now my question is, if I just erase the first element, will it cleanly destroy the object that was stored in the first element?

So:

Expand|Select|Wrap|Line Numbers
  1.                  servObj.erase(itservObj);        
  2.  
here itservObj is a vector::iterator. Will this deallocate the memory properly?

I havnt been coding in C++ for that long(read: never had to bother with memory management) so I would appreciate anyone pushing me in the right direction here.

Thanks in advance
Aug 3 '08 #1
2 2221
weaknessforcats
9,208 Expert Mod 8TB
Have you run this in the debugger to see of the CGame destructor is called?

What you have stored in the vector is a pointer to a heap object. Erasing the pointer in the vector does not delete the object.

The vector::at returns a reference to the pointer. Therefore, you should be deleting servObj.at(0), which is your pointer.
Aug 3 '08 #2
Ok I just tried again, and for some reason it works now where it didnt work before(I guess I missed something in my first try).

For anyone who cares:

Expand|Select|Wrap|Line Numbers
  1.  for( itservObj=l_servObj.begin(); itservObj != l_servObj.end(); itservObj++)
  2.                 {
  3.                         if( (**itservObj).whoareyou(serversh) == true )
  4.                         {
  5.                                 delete (*itservObj);
  6.                                 l_servObj.erase(itservObj);
  7.                         }
  8.  
  9.  
Dont know why this didnt work the first try, but thanks for your help anyway :)
Aug 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: da Vinci | last post by:
I have been trying to post to .help all day but nothing is going through. Will try it here and hope it does.... I am trying to access a getData() method that simply returns a stored integer...
8
by: Maximus | last post by:
Hi, I was wondering if it is bad to overuse the new & delete operator. In my application, e.g. I created my own list class and I will have to resize my variable maybe like 100 times during runtime...
16
by: cppaddict | last post by:
Hi, I am deleting some objects created by new in my class destructor, and it is causing my application to error at runtime. The code below compiles ok, and also runs fine if I remove the body...
1
by: Michael Jasn | last post by:
I had a question about memory management. Please look at the two functions below. Can you answer the two questions in the comments below. Thanks so much. -Mike func1(std::vector<Point>&...
11
by: DamonChong | last post by:
Hi, I am new to c++. I recently spend an enormous among of time troubleshooting a seeminly innocuous piece of code. Although I narrow down this piece of code as the culprit but I don't...
35
by: Jon Slaughter | last post by:
I'm having a problem allocating some elements of a vector then deleting them. Basicaly I have something like this: class base { private: std::vector<object> V;
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
6
by: nembo kid | last post by:
I have these classes: Boss, CommissionWorker, PieceWorker, HourlyWorker all directly derived from Employeer. I create a dynamic object (on heap) for each class:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.