简体   繁体   English

用C ++进行内存管理?

[英]Memory management in C++?

I'm developing a simple vehicle routing problem algorithm and I have a little problem with memory management. 我正在开发一种简单的车辆路径问题算法,但是内存管理方面存在一些问题。 So I have following classes 所以我有以下课程

In this one I store data of an Order 在此,我存储订单的数据

Order::Order(int id, int idLoc, double xCoord, double yCoord, double demand,  int startTime, int endTime, int serviceTime) {
    this->id = id;
    this->idLoc = idLoc;
    this->xCoord = xCoord;
    this->yCoord = yCoord;
    this->demand = demand;
    this->startTime = startTime;
    this->endTime = endTime;
    this->serviceTime = serviceTime;
}

Order::~Order() {
    // TODO Auto-generated destructor stub
}

In this one I store a list of Orders which represent a sequence of customers to serve during the distribution. 在此文件中,我存储了一系列订单,这些订单代表了在分发过程中要服务的一系列客户。

Route::Route(int idRoute,Vehicle *vehicleRoute) {
    id = idRoute;
    vehicle = vehicleRoute;
    demandRoute = -1;
    serviceTimeRoute =-1;
    earliestTimeRoute =-1;
    latestTimeRoute = -1;
    serviceDistanceRoute = -1;

    orders = new std::deque<Order>;
}

Route::Route(int idRoute, std::deque<Vehicle>::iterator vehicleIterator) {
    id = idRoute;
    vehicle = &(*vehicleIterator);
    demandRoute = -1;
    serviceTimeRoute =-1;
    earliestTimeRoute =-1;
    latestTimeRoute = -1;
    serviceDistanceRoute = -1;

    orders = new std::deque<Order>;
}

Route::~Route() {
    std::cout << "Destroying Route " << id  << std::endl;
    delete orders;
    std::cout << "Route Destroyed" << std::endl;
}

Finally, here I store a set of routes. 最后,我在这里存储了一组路线。

Solution::Solution() {
    solution = new std::deque<Route>;
}

Solution::~Solution() {
    std::cout << "Destroying Solution" << std::endl;
    delete solution;
    std::cout << "Solution Destroyed" << std::endl;
}

The problem is here: 问题在这里:

readCustomers(&orderList, &depotList);
readVehicles(&vehicleList);

std::deque<Vehicle>::iterator it = vehicleList.getVehicleList()->begin();

int routeID = 0;
Route route (routeID, it);

solution.getSolution()->push_back(route); //Problem

So I read Orders and Vehicles then I create Route route which has no Orders associated yet and finally I add this route to a solution. 因此,我阅读了“订单和车辆”,然后创建了尚未关联任何“订单”的Route路线,最后将此路线添加到解决方案中。

When I compile this code I got following output: 当我编译这段代码时,我得到以下输出:

Route address: 0x7fff7351e110
Solution's route address: 0x1ab8270
Destroying Route 0
Route Destroyed
Destroying Solution
Destroying Route 0
Route Destroyed
Solution Destroyed

*** Error in `/home/.../': corrupted double-linked list: 0x0000000001ab5800 ***

Used valgrind to gather more information about this error, got folowing messages: 使用valgrind收集有关此错误的更多信息,得到以下消息:

IVR [1]: Invalid read of size 8
IVF [1]: Invalid free()/delete/delete[]/realloc()

I guess I am releasing same chunk of memory twice. 我想我要释放两次相同的内存。 But I don't see where. 但是我看不到哪里。 Can any one help me with this one. 谁能帮我这个忙。

Route objects get copied when used in a std::deque. 在std :: deque中使用时,路由对象将被复制。

I see no Copy operator and you allocate the orders with new. 我看不到“复制”运算符,您分配了新的订单。 When a Route is now copied, the pointer is copied too. 现在复制路径时,指针也被复制。 So you have two objects pointing to the same object and freeing the same memory. 因此,您有两个对象指向同一对象并释放相同的内存。

Why are you using new and delete here. 您为什么在这里使用new和delete。 Embed the std::deque directly as a member and avoid new/delete. 直接将std :: deque嵌入为成员,并避免使用new / delete。

If you need new/delete: 如果需要新的/删除的:

  1. Try to use smart pointers 尝试使用智能指针
  2. Implement a copy constructor and copy operator. 实现复制构造函数和复制运算符。

If you decide to add it as a member you need to declare it in the class: 如果决定将其添加为成员,则需要在类中声明它:

class Route ...
{
   ...
   std::deque<Order> orders;

There is no need for new delete at all. 完全不需要新的删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM