why my program won’t stop while using -fsanitize=leak?

I write a simple queue implements by array and when I test it, I faced a wired problem.

// vector_queue.h

#ifndef TEST_VECTOR_QUEUE_H
#define TEST_VECTOR_QUEUE_H

#include "linked_queue.h"

template<class T>
class vector_queue {
public:
    explicit vector_queue(size_t capacity = default_capacity)
            : capacity(capacity), queue(new T[capacity]), head(0), tail(0) {}

    ~vector_queue() {
        delete[] queue;
        std::cout << "~" << std::endl;
    }

    [[nodiscard]] bool empty() const { return head == tail; }

    T dequeue() {
        if (empty()) {
            std::cout << "The queue is empty!" << std::endl;
            return 0;
        }

        T ret = queue[head];
        head = (head + 1) % capacity;
        return ret;
    }

    void enqueue(const T &x) {
        if (full()) {
            std::cout << "The queue is full!" << std::endl;
            return;
        }
        queue[tail] = x;
        tail = (tail + 1) % capacity;
    }

    [[nodiscard]] bool full() const { return head == (tail + 1) % capacity; }


private:
    static const size_t default_capacity;
    T *queue;           
    size_t head, tail;
    size_t capacity;

public:
    vector_queue &operator+(const T &x) {
        enqueue(x);
        return *this;
    }

    T operator--() {
        return this->dequeue();
    }

    vector_queue &operator=(const vector_queue &rhs) {
        if (this != &rhs) {
            this->queue = rhs.queue;
            this->head = rhs.head;
            this->tail = rhs.tail;
            this->capacity = rhs.capacity;
        }
        return *this;
    }

    friend std::ostream &operator<<(std::ostream &os, const vector_queue &q) {
        for (int i = q.head;; i = (i + 1) % q.capacity) {
            if (i == q.tail)
                break;
            os << q.queue[i] << " ";
        }
        os << std::endl;
        return os;
    }

    friend vector_queue &operator+(const T &x, vector_queue &q) {
        q.enqueue(x);
        return q;
    }
};


template<typename T>
const size_t vector_queue<T>::default_capacity = 4;


#endif //TEST_VECTOR_QUEUE_H
#include <iostream>
#include "vector_queue.h"

using Queue = vector_queue<int>;
using std::cout;
using std::endl;

int main() {
    Queue q;

    --q;                                  // Display:The queue is empty!
    q = q + 5 + 6 + 3;
    cout << q;                      // Display:5   6   3
    cout << --q << endl;      // Display:5
    cout << q;                      // Display:6   3
    q = 9 + q;
    cout << q;                      // Display:6   3   9
    q = 2 + q;                      // Display:The queue is full!
    cout << q;                     // Display:6   3   9
    return 0;
}

I use -fsanitize=leak in my cmake configuration. After displaying all the messages, it won't stop. If I remove -fsanitize=leak, everything will be fine, or if I delete q = q + 5 + 6 + 3; it will be fine as well. I am quite curious about it!

my device: MacBook Pro m1, macOS 11.3, compiler: clang version 12.0.0 which installed by homebrew.



Read more here: https://stackoverflow.com/questions/67377815/why-my-program-wont-stop-while-using-fsanitize-leak

Content Attribution

This content was originally published by MrZ at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: