C++ xstring _Large_string_engaged() const exception

给你一囗甜甜゛ 提交于 2021-01-29 20:35:52

问题


Hope everyone is doing ok. So I am making a simulation of a shop where a customer enters a shop, does shopping, cashes out and then de-spawns. A customer is spawned after a random amount of time. I am using Linked List to store these customers. This program uses SFML for graphics. So I wanted to test my code to have a look at my progress and in order to speed things up, I reduced the amount of time taken for a customer to spawn, the customers started spawning at a fast rate. So somewhere along the program when there are so many customers at a time, my program stops and gives me the following exception from the xstring header file:

bool _Large_string_engaged() const {
    return _BUF_SIZE <= _Myres;
}

This exception only appears when a lot of customers have spawned inside the shop at a time. However I think this exception isn't due to spawning a lot of them but in fact when it comes to deleting them or something. Here is the part of the code after which I started facing this error:

void CashOutCustomer() {
        SPointer = head;
        while (SPointer) {
            SPointer->customer.customer.setTexture(SPointer->customer._customer);
            SPointer->customer.CashOut(&cashier1, &cashier2, &cashier3);
            if (SPointer->customer.DeleteTime) {
                this->DeleteCustomer(SPointer); Count--; cout << "Count: " << Count << endl;
            }
            SPointer = SPointer->next;
        }
    }

    void DeleteCustomer(ShopStruct *S) {            //Fix deleting
        ShopStruct *TempNext, *TempPrev, *TempDelete;
        if (head == S) {
            TempNext = S->next; head = TempNext;
            TempDelete = S; S = head; 
            head->previous = nullptr; TempDelete->customer.~Customer();
        }
        else if(S->next == nullptr) {
            TempDelete = S; S = S->previous; S->next = nullptr; TempDelete->customer.~Customer();
        }
        else {
            TempNext = S->next; TempPrev = S->previous; TempDelete = S;
            TempNext->previous = TempPrev; TempPrev->next = TempNext;
            S = TempPrev; TempDelete->customer.~Customer();
        }
    }

Some points to note:

These functions keep running in a while loop. This while loop is for SFML to keep the graphics window running.

I ONLY face this exception when I speed up the spawning of the customers.

My guess is that the program is too fast due to the SFML while loop because of which the memory gets filled up quickly or something. However I am not sure so I would like to know why and when this exception appears. What does this exception represent?

I appreciate all the help. Thank you!

EDIT: It seems that this exception appears whenever I speed up the spawning of the customers. It has nothing to do with the deletion of customers. So I am going to post the whole class of the shop (which is a linked list):

struct ShopStruct {
Customer customer;
ShopStruct *next, *previous;
};

class Shop {
private:
    ShopStruct *head, *SPointer;
    int ShopLimit; float SpawnTime;
    Clock clock;

public:
    int Count, g = 0;
    Aisle aisle1, aisle2, aisle3;
    Cashier cashier1, cashier2, cashier3;

    Shop() : aisle1("A", "B", 1, 2), aisle2("C", "D", 3, 4), aisle3("E", "F", 5, 6) {
        head = nullptr; Count = 0;  ShopLimit = 20; SpawnTime = 0;
        srand(time(0));
    }

    void CreateCustomer() {
        if (!head) {
            head = new ShopStruct;
            head->previous = head->next = nullptr; Count++;
        }
        else {
            Customer c;
            ShopStruct* NShop = new ShopStruct; NShop->next = nullptr; NShop->customer = c;
            SPointer = head;
            while (SPointer->next)
                SPointer = SPointer->next;
            NShop->previous = SPointer; SPointer->next = NShop;
            Count++;
        }
    }

    void SpawnCustomer(bool isOpen) {
        float y;
        y = clock.getElapsedTime().asSeconds();
        if (g == 0 || !isOpen) {
            SpawnTime = rand() % 2; //change to 16 and 7
            clock.restart(); g = 1;
        }
        else if (Count <= 30 && isOpen && y >= SpawnTime) {
            this->CreateCustomer(); g = 0; cout << "Count: " << Count << endl;
        }
    }

    void CustomerShop() {
        SPointer = head;
        while (SPointer) {
            SPointer->customer.customer.setTexture(SPointer->customer._customer);
            SPointer->customer.WalkShop(&aisle1, &aisle2, &aisle3, &cashier1, &cashier2, &cashier3);
            SPointer = SPointer->next;
        }
    }

    void drawCustomer(RenderWindow *M) {
        Sprite temp;
        SPointer = head;
        while (SPointer) {
            M->draw(SPointer->customer.customer);
            SPointer = SPointer->next;
        }
    }

    void Restock(RenderWindow *M) {
        aisle1.RestockStack(1285, 1530, 230, "A", "B", 1, 2, M);
        aisle2.RestockStack(1285, 1530, 495, "C", "D", 3, 4, M);
        aisle3.RestockStack(1285, 1530, 785, "E", "F", 5, 6, M);
    }

    void CashOutCustomer() {
        SPointer = head;
        while (SPointer) {
            SPointer->customer.customer.setTexture(SPointer->customer._customer);
            SPointer->customer.CashOut(&cashier1, &cashier2, &cashier3);
            if (SPointer->customer.DeleteTime) {
                this->DeleteCustomer(SPointer); Count--; cout << "Count: " << Count << endl;
            }
            SPointer = SPointer->next;
        }
    }

    void DeleteCustomer(ShopStruct *S) {            //Fix deleting
        ShopStruct *TempNext, *TempPrev, *TempDelete;
        if (head == S) {
            TempNext = S->next; head = TempNext;
            TempDelete = S; S = head; 
            head->previous = nullptr; TempDelete->customer.~Customer();
        }
        else if(S->next == nullptr) {
            TempDelete = S; S = S->previous; S->next = nullptr; TempDelete->customer.~Customer();
        }
        else {
            TempNext = S->next; TempPrev = S->previous; TempDelete = S;
            TempNext->previous = TempPrev; TempPrev->next = TempNext;
            S = TempPrev; TempDelete->customer.~Customer();
        }
    }
 };

来源:https://stackoverflow.com/questions/65511929/c-xstring-large-string-engaged-const-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!