GithubHelp home page GithubHelp logo

Confused on something about cpr HOT 3 OPEN

bleriaf avatar bleriaf commented on May 19, 2024
Confused on something

from cpr.

Comments (3)

COM8 avatar COM8 commented on May 19, 2024

Sorry for the delayed response. I've been on vacation :)

In theory curl_easy_cleanup gets called when the cpr::Session and corresponding cpr::Reponse objects are destructed.
Ref:

curl_easy_cleanup(handle);

In my small test case curl_easy_cleanup is called properly every time:

TEST(SessionGetTests, GetSessionDestructor) {
    {
        Url url{server->GetBaseUrl() + "/hello.html"};
        Session session;
        session.SetUrl(url);
        std::string expected_text{"Hello world!"};
        Response response = session.Get();
        EXPECT_EQ(expected_text, response.text);
        EXPECT_EQ(url, response.url);
        EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
        EXPECT_EQ(200, response.status_code);
        EXPECT_EQ(ErrorCode::OK, response.error.code);
    }

    {
        Url url{server->GetBaseUrl() + "/hello.html"};
        Session session;
        session.SetUrl(url);
        std::string expected_text{"Hello world!"};
        Response response = session.Get();
        EXPECT_EQ(expected_text, response.text);
        EXPECT_EQ(url, response.url);
        EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
        EXPECT_EQ(200, response.status_code);
        EXPECT_EQ(ErrorCode::OK, response.error.code);
    }
}

Can you provide an example for reproducing it?

from cpr.

bleriaf avatar bleriaf commented on May 19, 2024

well i guess it does because the program closes once it finishes doing both, what if you try doing this does the cleanup still get called?

int main() {
        Session session;
        session.SetUrl(cpr::Url{ "https://google.com" } );
        Response response = session.Get();
        printf( "ret: %s ", response.text.c_str( ) );

       while ( true ) {
              Sleep( 1000 );
       }
}

from cpr.

COM8 avatar COM8 commented on May 19, 2024

Your example is highly dependent on your compile optimisation level. You compiler might catch the while infinite loop and never call the destructors.

A better solution would be:

int main() {
        {
            Session session;
            session.SetUrl(cpr::Url{ "https://google.com" } );
            Response response = session.Get();
            printf( "ret: %s ", response.text.c_str( ) );
        }

       while ( true ) {
              Sleep( 1000 );
       }
}

from cpr.

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.