Code Monkey home page Code Monkey logo

2d-viewer's People

Contributors

nikolausrauch avatar

Watchers

 avatar

2d-viewer's Issues

Memory Corruption on MacOS

In your demo, there's a code passage at

2D-viewer/demo/demo.cpp

Lines 71 to 77 in 9bb5068

viewer.drawCircles(circles.begin(), circles.end(), [](auto& c, auto& pos, auto& radius, auto& color)
{
pos = c.first;
radius = c.second;
color = {0.0, 1.0, 0.0, 1.0};
return true;
});

When executed on a MacBook (with an M1 processor), it leads to the following error:

demo(55959,0x1dc8fd000) malloc: Incorrect checksum for freed object 0x14a608310: probably modified after being freed.
Corrupt value: 0x343bbd2e3f800000
demo(55959,0x1dc8fd000) malloc: *** set a breakpoint in malloc_error_break to debug
[1]    55959 abort      ./bin/demo

It appears that the initialization of the new vector (vertices) in this function is broken on MacOS. Specifically, referring to

std::vector<glm::vec2> vertices(mRender.circleVertices);
for(unsigned int i = 0; i <= mRender.circleVertices; i++)
{
vertices[i] =
{
radius * glm::cos(2.0*glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices),
radius * glm::sin(2.0*glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices)
};
}

There could be two (or more) ways (for now) to fix this:

You could do something like:

#ifndef __APPLE__
std::vector<glm::vec2> vertices(mRender.circleVertices);
for (unsigned int i = 0; i <= mRender.circleVertices; i++) {
    vertices[i] = {
        radius * glm::cos(2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices),
        radius * glm::sin(2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices)};
}
#endif

...

for (unsigned int i = 0; i <= mRender.circleVertices; i++) {
#ifdef __APPLE__
    glVertex2f(position.x + radius * radius * glm::cos(2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices),
                       position.y + radius * radius * glm::sin(2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices));
#else
    glVertex2f(position.x + radius * vertices[i].x,
                       position.y + radius * vertices[i].y);
#endif
}

Or, without the #ifdef:

...
for (unsigned int i = 0; i <= mRender.circleVertices; i++) {
    const float angle = 2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices;  // For readability purposes
    glVertex2f(position.x + radius * radius * glm::cos(angle),
                       position.y + radius * radius * glm::sin(angle));
}

As you mentioned, you're not exactly sure why this happens, but with these changes, it wouldn't occur anymore. Additionally, you questioned why the vertices array is used in the first place, since, from your understanding, these vertices are only used once.

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.