Giter Club home page Giter Club logo

Comments (6)

f2404 avatar f2404 commented on June 29, 2024 1

Ok, accepted. Await another PR soon :)

from collections-c.

srdja avatar srdja commented on June 29, 2024 1

Awesome :)

from collections-c.

srdja avatar srdja commented on June 29, 2024

I don't think it's much of a problem since the user would have to pass an out to slist_iter_next anyway and would thus have a reference to free.

...
void *e;
while (slist_iter_next(iter, &e) != CC_ITER_END) {
    ...
    slist_iter_remove(iter, NULL); // Even though the output is ignored the user still has
                                   // access to the removed reference within the iterator context
}

Basically iter_remove removes the element that was already returned by the iter_next and it can't operate on any other value so it should be safe to ignore its out parameter.

from collections-c.

f2404 avatar f2404 commented on June 29, 2024

Maybe I'm missing something... but it seems to me that the caller cannot access the data of the removed element due to these lines:

    void *e = unlink(iter->list, iter->current, iter->prev);
    iter->current = NULL;

The only option to store the pointer to data is to provide 'out'.

from collections-c.

f2404 avatar f2404 commented on June 29, 2024

Basically, I started digging into this because valgrind is reporting memory leaks when running 'slist_test'. Some of them are internal to the test code, but others lead to the functions like slist_iter_remove.

from collections-c.

srdja avatar srdja commented on June 29, 2024

Technically true because after you call iter_remove you can no longer access the data from the list, but in practice iter_next gives you the data pointer before you get a chance to remove it from the list. iter_remove's out just returns the same thing that iter_next out returned and that's why it's not really a big deal if you ignore it or not.

I also ran valgrind and it seems that the problems are actually coming from slist_1234, or to be more precise not freeing its malloced data properly. Usually this list is freed by calling slist_destroy_free which also calls free on the data (and sometimes it's not freed at all :-), but since some of the malloced values are removed from the list, slist_destroy_free can't reach them. This is why the problem mostly comes from tests that call remove.

This can be fixed by manualy freeing the data after the remove:

    int *e;
    while (slist_iter_next(&iter, (void*) &e) != CC_ITER_END) {
        if (*e == 3) {
            slist_iter_remove(&iter, NULL);
            free(e);  // don't let data float around in interdimensional space
        }
    }

from collections-c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.