Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-34260: fix c code for memory leak #931

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions source/c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
MongoDB C Driver
================

.. facet::
:name: programming_language
:values: c

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: code example, get started, sample app

.. contents:: On this page
:local:
:backlinks: none
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
#include <mongoc/mongoc.h>

int main (int argc, char **argv)
{
mongoc_client_t *client = NULL;
bson_error_t error = {0};
mongoc_database_t *database = NULL;
bson_t *command = NULL, reply;


// Initialize the MongoDB C Driver.
mongoc_init ();

// Replace the <connection string> with your MongoDB deployment's connection string.
client = mongoc_client_new("<connection string>");

// Get a handle on the "admin" database.
database = mongoc_client_get_database (client, "admin");

// Ping the database.
command = BCON_NEW("ping", BCON_INT32(1));
if (mongoc_database_command_simple(database, command, NULL, &reply, &error))
{
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
}
else
{
// Error condition.
printf("Error: %s\n", error.message);
return 0;
}


// Perform Cleanup.
bson_destroy (&reply);
bson_destroy (command);
mongoc_database_destroy (database);
mongoc_client_destroy (client);
mongoc_cleanup ();

return 0;
}
int main(void) {
mongoc_client_t *client = NULL;
bson_error_t error = {0};
mongoc_database_t *database = NULL;
bson_t *command = NULL;
bson_t reply = BSON_INITIALIZER;
int rc = 0;
bool ok = true;

// Initialize the MongoDB C Driver.
mongoc_init();

client = mongoc_client_new("<connection string>");
if (!client) {
fprintf(stderr, "Failed to create a MongoDB client.\n");
rc = 1;
goto cleanup;
}

// Get a handle on the "admin" database.
database = mongoc_client_get_database(client, "admin");
if (!database) {
fprintf(stderr, "Failed to get a MongoDB database handle.\n");
rc = 1;
goto cleanup;
}

// Ping the database.
command = BCON_NEW("ping", BCON_INT32(1));
ok = mongoc_database_command_simple(
database, command, NULL, &reply, &error
);
if (!ok) {
fprintf(stderr, "error: %s\n", error.message);
rc = 1;
goto cleanup;
}
bson_destroy(&reply);

printf("Pinged your deployment. You successfully connected to MongoDB!\n");

// Perform cleanup.
cleanup:
bson_destroy(command);
mongoc_database_destroy(database);
mongoc_client_destroy(client);
mongoc_cleanup();

return rc;
}
114 changes: 66 additions & 48 deletions source/includes/connection-snippets/scram/c-connection.c
Original file line number Diff line number Diff line change
@@ -1,51 +1,69 @@
#include <mongoc/mongoc.h>

int main (int argc, char **argv)
{
mongoc_client_t *client = NULL;
bson_error_t error = {0};
mongoc_server_api_t *api = NULL;
mongoc_database_t *database = NULL;
bson_t *command = NULL, reply;

// Initialize the MongoDB C Driver.
mongoc_init ();

// Replace the <connection string> with your MongoDB deployment's connection string.
client = mongoc_client_new("<connection string>");

// Set the version of the Stable API on the client.
api = mongoc_server_api_new (MONGOC_SERVER_API_V1);
if (!mongoc_client_set_server_api (client, api, &error))
{
// Error condition.
printf("Error: %s\n", error.message);
return 0;
}

// Get a handle on the "admin" database.
database = mongoc_client_get_database (client, "admin");

// Ping the database.
command = BCON_NEW("ping", BCON_INT32(1));
if (mongoc_database_command_simple(database, command, NULL, &reply, &error))
{
int main(void) {
mongoc_client_t *client = NULL;
bson_error_t error = {0};
mongoc_server_api_t *api = NULL;
mongoc_database_t *database = NULL;
bson_t *command = NULL;
bson_t reply = BSON_INITIALIZER;
int rc = 0;
bool ok = true;

// Initialize the MongoDB C Driver.
mongoc_init();

client = mongoc_client_new("<connection string>");
if (!client) {
fprintf(stderr, "Failed to create a MongoDB client.\n");
rc = 1;
goto cleanup;
}

// Set the version of the Stable API on the client.
api = mongoc_server_api_new(MONGOC_SERVER_API_V1);
if (!api) {
fprintf(stderr, "Failed to create a MongoDB server API.\n");
rc = 1;
goto cleanup;
}

ok = mongoc_client_set_server_api(client, api, &error);
if (!ok) {
fprintf(stderr, "error: %s\n", error.message);
rc = 1;
goto cleanup;
}

// Get a handle on the "admin" database.
database = mongoc_client_get_database(client, "admin");
if (!database) {
fprintf(stderr, "Failed to get a MongoDB database handle.\n");
rc = 1;
goto cleanup;
}

// Ping the database.
command = BCON_NEW("ping", BCON_INT32(1));
ok = mongoc_database_command_simple(
database, command, NULL, &reply, &error
);
if (!ok) {
fprintf(stderr, "error: %s\n", error.message);
rc = 1;
goto cleanup;
}
bson_destroy(&reply);

printf("Pinged your deployment. You successfully connected to MongoDB!\n");
}
else
{
// Error condition.
printf("Error: %s\n", error.message);
return 0;
}


// Perform Cleanup.
bson_destroy (&reply);
bson_destroy (command);
mongoc_database_destroy (database);
mongoc_client_destroy (client);
mongoc_cleanup ();

return 0;
}

// Perform cleanup.
cleanup:
bson_destroy(command);
mongoc_database_destroy(database);
mongoc_server_api_destroy(api);
mongoc_client_destroy(client);
mongoc_cleanup();

return rc;
}
Loading