Skip to content

Commit 0418d3b

Browse files
author
Edward Thomson
authored
Merge pull request libgit2#4078 from pks-t/pks/example-cleanup
Fix general example memory leaks
2 parents 44e8af8 + f9ea8c6 commit 0418d3b

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

examples/general.c

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ static void object_database(git_repository *repo, git_oid *oid)
247247
*/
248248
git_oid_fmt(oid_hex, oid);
249249
printf("Written Object: %s\n", oid_hex);
250+
251+
/**
252+
* Free the object database after usage.
253+
*/
254+
git_odb_free(odb);
250255
}
251256

252257
/**
@@ -264,7 +269,7 @@ static void commit_writing(git_repository *repo)
264269
git_oid tree_id, parent_id, commit_id;
265270
git_tree *tree;
266271
git_commit *parent;
267-
const git_signature *author, *cmtter;
272+
git_signature *author, *committer;
268273
char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
269274

270275
printf("\n*Commit Writing*\n");
@@ -276,9 +281,9 @@ static void commit_writing(git_repository *repo)
276281
* `user.email` configuration options. See the `config` section of this
277282
* example file to see how to access config values.
278283
*/
279-
git_signature_new((git_signature **)&author,
284+
git_signature_new(&author,
280285
"Scott Chacon", "schacon@gmail.com", 123456789, 60);
281-
git_signature_new((git_signature **)&cmtter,
286+
git_signature_new(&committer,
282287
"Scott A Chacon", "scott@github.com", 987654321, 90);
283288

284289
/**
@@ -301,7 +306,7 @@ static void commit_writing(git_repository *repo)
301306
repo,
302307
NULL, /* do not update the HEAD */
303308
author,
304-
cmtter,
309+
committer,
305310
NULL, /* use default message encoding */
306311
"example commit",
307312
tree,
@@ -312,6 +317,14 @@ static void commit_writing(git_repository *repo)
312317
*/
313318
git_oid_fmt(oid_hex, &commit_id);
314319
printf("New Commit: %s\n", oid_hex);
320+
321+
/**
322+
* Free all objects used in the meanwhile.
323+
*/
324+
git_tree_free(tree);
325+
git_commit_free(parent);
326+
git_signature_free(author);
327+
git_signature_free(committer);
315328
}
316329

317330
/**
@@ -431,7 +444,11 @@ static void tag_parsing(git_repository *repo)
431444
printf("Tag Name: %s\nTag Type: %s\nTag Message: %s\n",
432445
name, git_object_type2string(type), message);
433446

447+
/**
448+
* Free both the commit and tag after usage.
449+
*/
434450
git_commit_free(commit);
451+
git_tag_free(tag);
435452
}
436453

437454
/**
@@ -485,9 +502,10 @@ static void tree_parsing(git_repository *repo)
485502
git_tree_entry_to_object(&obj, repo, entry); /* blob */
486503

487504
/**
488-
* Remember to close the looked-up object once you are done using it
505+
* Remember to close the looked-up object and tree once you are done using it
489506
*/
490507
git_object_free(obj);
508+
git_tree_free(tree);
491509
}
492510

493511
/**
@@ -522,6 +540,11 @@ static void blob_parsing(git_repository *repo)
522540
* */
523541
printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); /* 8 */
524542
git_blob_rawcontent(blob); /* "content" */
543+
544+
/**
545+
* Free the blob after usage.
546+
*/
547+
git_blob_free(blob);
525548
}
526549

527550
/**
@@ -644,10 +667,7 @@ static void index_walking(git_repository *repo)
644667
static void reference_listing(git_repository *repo)
645668
{
646669
git_strarray ref_list;
647-
const char *refname;
648-
git_reference *ref;
649670
unsigned i;
650-
char oid_hex[GIT_OID_HEXSZ+1];
651671

652672
printf("\n*Reference Listing*\n");
653673

@@ -662,7 +682,10 @@ static void reference_listing(git_repository *repo)
662682
git_reference_list(&ref_list, repo);
663683

664684
for (i = 0; i < ref_list.count; ++i) {
665-
memset(oid_hex, 0, sizeof(oid_hex));
685+
git_reference *ref;
686+
char oid_hex[GIT_OID_HEXSZ+1] = GIT_OID_HEX_ZERO;
687+
const char *refname;
688+
666689
refname = ref_list.strings[i];
667690
git_reference_lookup(&ref, repo, refname);
668691

@@ -679,6 +702,8 @@ static void reference_listing(git_repository *repo)
679702
fprintf(stderr, "Unexpected reference type\n");
680703
exit(1);
681704
}
705+
706+
git_reference_free(ref);
682707
}
683708

684709
git_strarray_free(&ref_list);
@@ -696,7 +721,7 @@ static void config_files(const char *repo_path, git_repository* repo)
696721
{
697722
const char *email;
698723
char config_path[256];
699-
int32_t j;
724+
int32_t autocorrect;
700725
git_config *cfg;
701726
git_config *snap_cfg;
702727

@@ -708,10 +733,16 @@ static void config_files(const char *repo_path, git_repository* repo)
708733
sprintf(config_path, "%s/config", repo_path);
709734
check_error(git_config_open_ondisk(&cfg, config_path), "opening config");
710735

711-
git_config_get_int32(&j, cfg, "help.autocorrect");
712-
printf("Autocorrect: %d\n", j);
736+
if (git_config_get_int32(&autocorrect, cfg, "help.autocorrect") == 0)
737+
printf("Autocorrect: %d\n", autocorrect);
713738

714739
check_error(git_repository_config_snapshot(&snap_cfg, repo), "config snapshot");
715740
git_config_get_string(&email, snap_cfg, "user.email");
716741
printf("Email: %s\n", email);
742+
743+
/**
744+
* Remember to free the configurations after usage.
745+
*/
746+
git_config_free(cfg);
747+
git_config_free(snap_cfg);
717748
}

0 commit comments

Comments
 (0)