不要在两个指针之间共享一个对象。或者,如果你这样做, 有一个指针拥有对象 - 永远不要试图通过 删除它的另一个指针。但这可能会变得复杂。你必须确定 主人没有删除它,而它仍然可以通过任何 其他指针访问。 一般情况下,您无法知道指针 指向的对象是否仍然存在,或者是否已被删除,以及任何尝试引用已删除的 对象的原因未定义的行为。 -Kevin - 我的电子邮件地址有效,但会定期更改。 要与我联系,请使用最近发布的地址。 是的。使用智能指针。然后,只需销毁你不再需要的指针副本。当对象的最后一个指针被销毁时, 将释放该对象。这是一个非常流行的智能指针: www.boost.org/libs/smart_ptr/index.htm Dave --- 外发邮件已通过无病毒验证。 由AVG反病毒系统检查( http://www.grisoft.com)。 版本:6.0.521 /病毒库:319 - 发布日期:2003年9月23日 ''toDie = NULL;''只将toDie指针设置为NULL,它不会影响 它指向的对象至。请注意,该对象不再存在于 ,因为它被设置为NULL,因为它在 之前被删除了。 - Peter van Merkerk peter.van.merkerk(at)dse.nl [见 http://www.gotw.ca/resources/clcm.htm 有关的信息] [comp.lang.c ++。moderated。第一次海报:做到这一点! ] I''m iterating over a vector of base class pointers and deleting thosewhich meet a certain criteria...i''m using pretty text-book code forthe particular delete/erasure (it''s straight out of Myers'' EffectiveSTL), and reads like this:void CGame::RemoveDeadObjects(){// cleanup crew!!vector<CDrawableObject*>::iterator i;for (i = g_vecGameObjects.begin(); i !=g_vecGameObjects.end();){// if object is not null and can be removed...if ((*i) && (*i)->CanRemove()){// grab hold before erasureCDrawableObject* toDie = *i;// erase...i = g_vecGameObjects.erase(i);// ...and killdelete toDie;toDie = NULL;}else++i;}}Here''s the problem: on the two lines that say delete toDie; toDie =NULL; I am noticing that the original variable to which toDie ispointing is not being set to NULL, just "toDie" itself.In other words, if one of the CDrawableObjects* was say, a member varof CGame called m_cdPlayer1, when the delete/set-to-NULL happens:1. Before the delete, toDie and m_cdPlayer1 have a value of0x00a72498.2. After the delete, both toDie and m_cdPlayer1 have their __vfptrpoint to memory 0xFEEEFEEE (signifying a delete)3. After the set-to-null, the value of toDie becomes 0x00000000, whilem_cdPlayer1 remains at a value of 0x00a72498 (while still pointing to0xFEEEFEEE).Not the behavior I want. At this point, m_cdPlayer1 is officiallydeleted, yet the test "if (m_cdPlayer)" fails, since it is technicallynot NULL.Any suggestions or recommendations on how to get the intended behaviorout of this snippet?- Hanzo[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 解决方案Don''t share an object between two pointers like that. Or, if you do,have one pointer "own" the object - never attempt to delete it throughthe other pointer. This can get complicated, though. You have to be surethe owner doesn''t delete it while it may still be accessed through anyother pointer.In general, you cannot know whether the object pointed to by a pointerstill exists, or has been deleted, and any attempt to refer to a deletedobject causes undefined behavior.-Kevin--My email address is valid, but changes periodically.To contact me please use the address from a recent posting.Yes. Use a smart pointer. Then, just destroy the copies of pointers thatyou don''t want any more. When the last pointer to the object is destroyed,the object will be freed. Here''s a pretty popular smart pointer: www.boost.org/libs/smart_ptr/index.htmDave---Outgoing mail is certified Virus Free.Checked by AVG anti-virus system (http://www.grisoft.com).Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003''toDie = NULL;'' only sets the toDie pointer to NULL it does not affectthe object it was pointing to. Note that that object no longer exists atthe point toDie is being set to NULL because it was deleted just beforethat line.--Peter van Merkerkpeter.van.merkerk(at)dse.nl[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 这篇关于从STL Vector中删除指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 19:52