Skip to content

Commit 8137656

Browse files
authored
DOCSP-34260: fix c code for memory leak (#931)
* DOCSP-34260: fix c code for memory leak * update connection strings * format errors
1 parent ff1ed5a commit 8137656

File tree

3 files changed

+127
-88
lines changed

3 files changed

+127
-88
lines changed

source/c.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
MongoDB C Driver
55
================
66

7+
.. facet::
8+
:name: programming_language
9+
:values: c
10+
11+
.. facet::
12+
:name: genre
13+
:values: reference
14+
15+
.. meta::
16+
:keywords: code example, get started, sample app
17+
718
.. contents:: On this page
819
:local:
920
:backlinks: none
Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
11
#include <mongoc/mongoc.h>
22

3-
int main (int argc, char **argv)
4-
{
5-
mongoc_client_t *client = NULL;
6-
bson_error_t error = {0};
7-
mongoc_database_t *database = NULL;
8-
bson_t *command = NULL, reply;
9-
10-
11-
// Initialize the MongoDB C Driver.
12-
mongoc_init ();
13-
14-
// Replace the <connection string> with your MongoDB deployment's connection string.
15-
client = mongoc_client_new("<connection string>");
16-
17-
// Get a handle on the "admin" database.
18-
database = mongoc_client_get_database (client, "admin");
19-
20-
// Ping the database.
21-
command = BCON_NEW("ping", BCON_INT32(1));
22-
if (mongoc_database_command_simple(database, command, NULL, &reply, &error))
23-
{
24-
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
25-
}
26-
else
27-
{
28-
// Error condition.
29-
printf("Error: %s\n", error.message);
30-
return 0;
31-
}
32-
33-
34-
// Perform Cleanup.
35-
bson_destroy (&reply);
36-
bson_destroy (command);
37-
mongoc_database_destroy (database);
38-
mongoc_client_destroy (client);
39-
mongoc_cleanup ();
40-
41-
return 0;
42-
}
3+
int main(void) {
4+
mongoc_client_t *client = NULL;
5+
bson_error_t error = {0};
6+
mongoc_database_t *database = NULL;
7+
bson_t *command = NULL;
8+
bson_t reply = BSON_INITIALIZER;
9+
int rc = 0;
10+
bool ok = true;
11+
12+
// Initialize the MongoDB C Driver.
13+
mongoc_init();
14+
15+
client = mongoc_client_new("<connection string>");
16+
if (!client) {
17+
fprintf(stderr, "Failed to create a MongoDB client.\n");
18+
rc = 1;
19+
goto cleanup;
20+
}
21+
22+
// Get a handle on the "admin" database.
23+
database = mongoc_client_get_database(client, "admin");
24+
if (!database) {
25+
fprintf(stderr, "Failed to get a MongoDB database handle.\n");
26+
rc = 1;
27+
goto cleanup;
28+
}
29+
30+
// Ping the database.
31+
command = BCON_NEW("ping", BCON_INT32(1));
32+
ok = mongoc_database_command_simple(
33+
database, command, NULL, &reply, &error
34+
);
35+
if (!ok) {
36+
fprintf(stderr, "error: %s\n", error.message);
37+
rc = 1;
38+
goto cleanup;
39+
}
40+
bson_destroy(&reply);
41+
42+
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
43+
44+
// Perform cleanup.
45+
cleanup:
46+
bson_destroy(command);
47+
mongoc_database_destroy(database);
48+
mongoc_client_destroy(client);
49+
mongoc_cleanup();
50+
51+
return rc;
52+
}
Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,69 @@
11
#include <mongoc/mongoc.h>
22

3-
int main (int argc, char **argv)
4-
{
5-
mongoc_client_t *client = NULL;
6-
bson_error_t error = {0};
7-
mongoc_server_api_t *api = NULL;
8-
mongoc_database_t *database = NULL;
9-
bson_t *command = NULL, reply;
10-
11-
// Initialize the MongoDB C Driver.
12-
mongoc_init ();
13-
14-
// Replace the <connection string> with your MongoDB deployment's connection string.
15-
client = mongoc_client_new("<connection string>");
16-
17-
// Set the version of the Stable API on the client.
18-
api = mongoc_server_api_new (MONGOC_SERVER_API_V1);
19-
if (!mongoc_client_set_server_api (client, api, &error))
20-
{
21-
// Error condition.
22-
printf("Error: %s\n", error.message);
23-
return 0;
24-
}
25-
26-
// Get a handle on the "admin" database.
27-
database = mongoc_client_get_database (client, "admin");
28-
29-
// Ping the database.
30-
command = BCON_NEW("ping", BCON_INT32(1));
31-
if (mongoc_database_command_simple(database, command, NULL, &reply, &error))
32-
{
3+
int main(void) {
4+
mongoc_client_t *client = NULL;
5+
bson_error_t error = {0};
6+
mongoc_server_api_t *api = NULL;
7+
mongoc_database_t *database = NULL;
8+
bson_t *command = NULL;
9+
bson_t reply = BSON_INITIALIZER;
10+
int rc = 0;
11+
bool ok = true;
12+
13+
// Initialize the MongoDB C Driver.
14+
mongoc_init();
15+
16+
client = mongoc_client_new("<connection string>");
17+
if (!client) {
18+
fprintf(stderr, "Failed to create a MongoDB client.\n");
19+
rc = 1;
20+
goto cleanup;
21+
}
22+
23+
// Set the version of the Stable API on the client.
24+
api = mongoc_server_api_new(MONGOC_SERVER_API_V1);
25+
if (!api) {
26+
fprintf(stderr, "Failed to create a MongoDB server API.\n");
27+
rc = 1;
28+
goto cleanup;
29+
}
30+
31+
ok = mongoc_client_set_server_api(client, api, &error);
32+
if (!ok) {
33+
fprintf(stderr, "error: %s\n", error.message);
34+
rc = 1;
35+
goto cleanup;
36+
}
37+
38+
// Get a handle on the "admin" database.
39+
database = mongoc_client_get_database(client, "admin");
40+
if (!database) {
41+
fprintf(stderr, "Failed to get a MongoDB database handle.\n");
42+
rc = 1;
43+
goto cleanup;
44+
}
45+
46+
// Ping the database.
47+
command = BCON_NEW("ping", BCON_INT32(1));
48+
ok = mongoc_database_command_simple(
49+
database, command, NULL, &reply, &error
50+
);
51+
if (!ok) {
52+
fprintf(stderr, "error: %s\n", error.message);
53+
rc = 1;
54+
goto cleanup;
55+
}
56+
bson_destroy(&reply);
57+
3358
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
34-
}
35-
else
36-
{
37-
// Error condition.
38-
printf("Error: %s\n", error.message);
39-
return 0;
40-
}
41-
42-
43-
// Perform Cleanup.
44-
bson_destroy (&reply);
45-
bson_destroy (command);
46-
mongoc_database_destroy (database);
47-
mongoc_client_destroy (client);
48-
mongoc_cleanup ();
49-
50-
return 0;
51-
}
59+
60+
// Perform cleanup.
61+
cleanup:
62+
bson_destroy(command);
63+
mongoc_database_destroy(database);
64+
mongoc_server_api_destroy(api);
65+
mongoc_client_destroy(client);
66+
mongoc_cleanup();
67+
68+
return rc;
69+
}

0 commit comments

Comments
 (0)