c - using a pointer returned from function in a function call

Oz123

Is the following usage of pointers in functions call is a memory leak:

bson_t * parse_json(const char * json_fields){

    bson_error_t error;
    bson_t *bson_fields = bson_new_from_json((unsigned char *)json_fields, -1, &error);
    if (!bson_fields) {
        log_die("Error: %s\n", error.message);
    } else {
      return bson_fields;
    }
    log_die("Error: something bad happend in parse_columns");
    return bson_fields; // this should never be reached ...
}

The following code works, but what happens to the pointer from parse_json here? Is this a memory leak?

bson_concat(fields, parse_json(json_fields));

The mongodb C-API offers the function bson_destory:

bson_destroy(fields);

I am wondering maybe it's better to explicitly free the memory of new_fields:

        bson_t *new_fields = parse_json(json_fields);
        bson_concat(fields, new_fields);
        bson_destroy(new_fields);

While this example uses mongodb c-api, I am also trying to understand the general case.

  some_type * pointer_returner(){
  some_type *var;
  ...

  return var;
  }


  do_something(pointer_retuner());

Is the call above causing a memory leak?

ouah

Yes, you need to call bson_destroy to deallocate your structure object it is no longer used.

From bson_destroy documentation:

The bson_destroy() function shall free an allocated bson_t structure.

This function should always be called when you are done with a bson_t unless otherwise specified.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling C function from returned pointer in NodeJS

From Dev

Call a function returned by a lua script from C

From Dev

C - values is struct pointer are not set after pointer is returned from function

From Dev

Assigning a pointer returned from a function

From Dev

Call C++ function pointer from Javascript

From Dev

Call a C function that returns a pointer from cython

From Dev

C++ call function from inside a function with pointer array

From Dev

How to pass a delegate or function pointer from C# to C++ and call it there using InternalCall

From

Assign value returned from function to pointer

From Dev

C: Pointer after function call

From Dev

No matching function for call, using a function pointer

From Dev

Call function by function pointer from array of structs

From Dev

How to call a function from a function pointer?

From Dev

Allocation function and pointer to returned

From Dev

Using ctypes to call C++ function with pointer args

From Dev

C++ Using lambda for implicit constructor call expecting a function pointer

From Dev

c++ pointer to pointer and call function that receive pointer to pointer

From Dev

Get returned char from a C function using inline assembly

From Dev

unexpected integer returned from a C function by calling it using ctypes

From Dev

Using ! in a function call in C

From Dev

how do i access an array returned as a pointer from a function using a subscript?

From Dev

C++: use std::string returned by a function: Using pointer to local variable that is out of scope

From Dev

How to call private member function by using a pointer

From Dev

Call a function using a pointer and pass it along in the parameters

From Dev

pointer being returned from a function from an SDL library

From Dev

C: malloc'ed pointer changes location after being returned from function?

From Dev

Call base method from inherited function pointer

From Dev

Get Function Pointer from Call Stack

From Dev

How to call a function from any class by a pointer to it?

Related Related

  1. 1

    Calling C function from returned pointer in NodeJS

  2. 2

    Call a function returned by a lua script from C

  3. 3

    C - values is struct pointer are not set after pointer is returned from function

  4. 4

    Assigning a pointer returned from a function

  5. 5

    Call C++ function pointer from Javascript

  6. 6

    Call a C function that returns a pointer from cython

  7. 7

    C++ call function from inside a function with pointer array

  8. 8

    How to pass a delegate or function pointer from C# to C++ and call it there using InternalCall

  9. 9

    Assign value returned from function to pointer

  10. 10

    C: Pointer after function call

  11. 11

    No matching function for call, using a function pointer

  12. 12

    Call function by function pointer from array of structs

  13. 13

    How to call a function from a function pointer?

  14. 14

    Allocation function and pointer to returned

  15. 15

    Using ctypes to call C++ function with pointer args

  16. 16

    C++ Using lambda for implicit constructor call expecting a function pointer

  17. 17

    c++ pointer to pointer and call function that receive pointer to pointer

  18. 18

    Get returned char from a C function using inline assembly

  19. 19

    unexpected integer returned from a C function by calling it using ctypes

  20. 20

    Using ! in a function call in C

  21. 21

    how do i access an array returned as a pointer from a function using a subscript?

  22. 22

    C++: use std::string returned by a function: Using pointer to local variable that is out of scope

  23. 23

    How to call private member function by using a pointer

  24. 24

    Call a function using a pointer and pass it along in the parameters

  25. 25

    pointer being returned from a function from an SDL library

  26. 26

    C: malloc'ed pointer changes location after being returned from function?

  27. 27

    Call base method from inherited function pointer

  28. 28

    Get Function Pointer from Call Stack

  29. 29

    How to call a function from any class by a pointer to it?

HotTag

Archive