2016-08-19 80 views
-1

我有内存泄漏问题。在名为“Deleaker”的程序中进行测试。 如果我注释掉析构函数的内容,它正确地处理这些内存泄漏。 谢谢你的帮助。内存泄漏类

Inventory.h

#ifndef INVENTORY_H 
#define INVENTORY_H 

#include "Item.h" 

class InventoryOfItems { 
public: 
    InventoryOfItems(int c = 10); 
    ~InventoryOfItems(); 

    bool Add(Item* newItem); 
    Item* Remove(int itemIndex); 
    int Size() const; 
    int Items() const; 
    int Weapons() const; 
    int Potions() const; 
    Item::item_t GetItemType(int invIndex) const; 

    void DisplayWeapons(); 
    void DisplayPotions(); 
    void DisplayInfo(); 
private: 
    Item** items; 
    int itemCount; 
    int capacity; 
}; 

#endif /* INVENTORY_H */ 

Inventory.cpp

#include "Inventory.h" 
#include "Weapon.h" 
#include "Potion.h" 

#include <iostream> 
using std::cout; 
using std::endl; 

#include <iomanip> 
using std::setw; 
using std::setiosflags; 
using std::ios; 

#include <string> 

InventoryOfItems::InventoryOfItems(int cap) { 
    itemCount = 0; 
    capacity = cap; 
    items = new Item*[capacity]; 

    for (int i = 0; i < capacity; i++) { 
     items[i] = 0; 
    } 
} 

InventoryOfItems::~InventoryOfItems() { 

    for (int i = 0; i < itemCount; i++) { 
     delete items[i]; 
    } 

    delete [] items; 
} 

bool InventoryOfItems::Add(Item* newItem) { 
    if (newItem == 0) 
     return false; 

    for (int i = 0; i < capacity; i++) 
     if (items[i] == 0) { // empty 
      switch (newItem->GetType()) { 

       case Item::Weapon: 
       { 

        Weapon* originalWeapon = (Weapon*) newItem; 
        Weapon* newWeapon = new Weapon(originalWeapon); 

        items[i] = newWeapon; 
        itemCount++; 
        break; 
       } 
       case Item::Potion: 
       { 
        Potion* originalPotion = (Potion*) newItem; 
        Potion* newPotion = new Potion(originalPotion); 
        items[i] = newPotion; 
        itemCount++; 
        break; 
       } 
      } 
      return true; 
     } 

    return false; 
} 

Item* InventoryOfItems::Remove(int itemIndex) { 
    if (items[itemIndex - 1] == 0) 
     return 0; 
    else { 
     Item* itemToReturn; 
     itemToReturn = items[itemIndex - 1]; 
     items[itemIndex - 1] = 0; 
     itemCount--; 
     return itemToReturn; 
    } 
} 

int InventoryOfItems::Size() const { 
    return capacity; 
} 

int InventoryOfItems::Items() const { 
    return itemCount; 
} 

int InventoryOfItems::Weapons() const { 
    int count = 0; 
    for (int i = 0; i < capacity; i++) 
     if (items[i] != 0) 
      if (items[i]->GetType() == Item::Weapon) 
       count++; 

    return count; 
} 

int InventoryOfItems::Potions() const { 
    int count = 0; 
    for (int i = 0; i < capacity; i++) 
     if (items[i] != 0) 
      if (items[i]->GetType() == Item::Potion) 
       count++; 

    return count; 
} 

Item::item_t InventoryOfItems::GetItemType(int invIndex) const { 
    if (items[invIndex - 1] == 0) 
     return Item::Undefined; 
    else 
     return items[invIndex - 1]->GetType(); 
} 

void InventoryOfItems::DisplayWeapons() { 
    cout << " WEAPONS" << endl; 
    for (int i = 0; i < capacity; i++) 
     if (items[i] != NULL) 
      if (items[i]->GetType() == Item::Weapon) { 
       cout << " " << setiosflags(ios::right) << setw(2) << i + 1 << ": "; 
       items[i]->DisplayInfo(); 
       cout << endl; 
      } 
} 

void InventoryOfItems::DisplayPotions() { 
    cout << " POTIONS" << endl; 
    for (int i = 0; i < capacity; i++) 
     if (items[i] != NULL) 
      if (items[i]->GetType() == Item::Potion) { 
       cout << " " << setiosflags(ios::right) << setw(2) << i + 1 << ": "; 
       items[i]->DisplayInfo(); 
       cout << endl; 
      } 
} 

void InventoryOfItems::DisplayInfo() { 
    cout << endl; 
    cout << " ----------------------------------" << endl; 
    cout << " Inventory (carrying " << itemCount << " of " << capacity << " items)" << endl; 
    cout << " ----------------------------------" << endl; 

    DisplayWeapons(); 
    DisplayPotions(); 
} 

调用remove:

void Hero::equip() 
{ 
    int item = 0; 

    if(inv.Items() == 0) 
    { 
     cout << "Your inventory is empty!" << endl; 
    } 
    else 
    { 
     inv.DisplayWeapons(); 
     cout << "Choose a weapon: " << endl; 
     cin >> item; 

     if(weapon != NULL) 
     { 
      inv.Add(weapon); 
     } 

     Weapon *selected = (Weapon*) inv.Remove(item); 
     weapon = new Weapon(selected); 
     delete selected; 
    } 
} 

武器是指向武器(英雄的手)

+2

看看3/5/0规则。 – Jarod42

+1

那么,如果您不评论析构函数,是否有问题? – Mike

+3

你应该使用'std :: vector >'。 – Jarod42

回答

0

你”不是在覆盖它之前,先写入-ing weapon。请注意添加一行:

inv.DisplayWeapons(); 
    cout << "Choose a weapon: " << endl; 
    cin >> item; 

    if(weapon != NULL) 
    { 
     inv.Add(weapon); 
     delete weapon; // Added 
    } 

    Weapon *selected = (Weapon*) inv.Remove(item); 
    weapon = new Weapon(selected); // `weapon` would be overwritten here 
    delete selected; 

话虽如此,我还建议使用std::vector代替所有这些指针,因为我们在这个问题评论说,因为搞乱了内存管理是很容易。

+0

我在类英雄的析构函数中删除了武器 –

+0

只有在英雄被破坏时才会调用这个函数。它不会从'Hero :: equip()'中被破坏。当一个对象被删除或超出范围时调用析构函数。你并没有删除你的'Hero'对象,也不会在它自己的函数内超出范围。 –

+0

仍然存在问题,库存析构..当我评论它的工作..当它不给我错误的对象..耶稣为什么 和是的我会用矢量..只是试图用矢量重写它。 ..:/ –