Connecting Tech Pros Worldwide Help | Site Map

STL

Tony Johansson
Guest
 
Posts: n/a
#1: Aug 20 '05
Hello experts!!

If I create object dynamically and store these pointers in a STL container
for example vector and when
this STL containers is destroyd the dynamically object will not be
destroyed.

What is the best solution to this kind of problem?

How is it possible to deallocate these object existing in a STL.

//Tony


Srini
Guest
 
Posts: n/a
#2: Aug 20 '05

re: STL


Use smart pointers... Boost's shared_ptr would be a good choice I
guess.

Srini

Tobias Blomkvist
Guest
 
Posts: n/a
#3: Aug 20 '05

re: STL


Tony Johansson sade:[color=blue]
> Hello experts!!
>
> If I create object dynamically and store these pointers in a STL container
> for example vector and when
> this STL containers is destroyd the dynamically object will not be
> destroyed.
>
> What is the best solution to this kind of problem?[/color]

Be aware of your design choices.
[color=blue]
>
> How is it possible to deallocate these object existing in a STL.
>[/color]

Iterate and delete.

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Greger
Guest
 
Posts: n/a
#4: Aug 21 '05

re: STL


Tony Johansson wrote:
[color=blue]
> Hello experts!!
>
> If I create object dynamically and store these pointers in a STL container
> for example vector and when
> this STL containers is destroyd the dynamically object will not be
> destroyed.
>
> What is the best solution to this kind of problem?
>
> How is it possible to deallocate these object existing in a STL.
>
> //Tony[/color]
have alook at auto_ptr in the STL, see header memory in your stl imp.
--
http://www.gregerhaga.net
Kai-Uwe Bux
Guest
 
Posts: n/a
#5: Aug 21 '05

re: STL


Greger wrote:
[color=blue]
> Tony Johansson wrote:
>[color=green]
>> Hello experts!!
>>
>> If I create object dynamically and store these pointers in a STL
>> container for example vector and when
>> this STL containers is destroyd the dynamically object will not be
>> destroyed.
>>
>> What is the best solution to this kind of problem?
>>
>> How is it possible to deallocate these object existing in a STL.
>>
>> //Tony[/color]
> have alook at auto_ptr in the STL, see header memory in your stl imp.[/color]

Observe that the OP was asking specifically about storing pointers to
objects in an STL container, i.e., he is dealing with something like:

std::vector< Car * >

It is *not feasible* to replace this by

std::vector< std::auto_ptr< Car > >

since std::auto_ptr does not satisfy the requirements for a type to be used
in std::vector: assignable, copy constructible, ...


To the OP: very likely a reference counting smart pointer class will work
for you.


Best

Kai-Uwe Bux
peter.koch.larsen@gmail.com
Guest
 
Posts: n/a
#6: Aug 21 '05

re: STL



Tony Johansson skrev:
[color=blue]
> Hello experts!!
>
> If I create object dynamically and store these pointers in a STL container
> for example vector and when
> this STL containers is destroyd the dynamically object will not be
> destroyed.
>
> What is the best solution to this kind of problem?
>
> How is it possible to deallocate these object existing in a STL.
>
> //Tony[/color]

In general, don't use raw pointers in a STL-container. The auto_ptr
suggestion mentioned by someone else is also not feasible and should
not even compile. The reason for advising against raw pointers is
related to the fact that this will prevent you from using some of the
algorithms available on those containers. Everything that removes items
is more or less guaranteed to cause a memory-leak.

/Peter

Closed Thread


Similar C / C++ bytes