I have this main which calls the asn1 wrappers for decoding and after encoding:
extern "C" {
#include "decoder.h"
#include "encoder.h"
}
int
main() {
void* structure = 0;
void* root_structure = 0;
ssize_t result = decode((void*)&(buffer_in),buffer_in_size,&structure,&root_structure);
if(result == -1) {
std::cout << "- Decoder returned size -1. Error during decoding." << std::endl;
break;
}
else {
encode(encodedBuffer,encodedBuffer_size,&structure);
}
clean_decoder_instances(&root_structure);
}
The main does not know the asn1 types, it just calls the two wrappers. Once it has finished the procedure, it should pass the structure (cast to void) to the function in the decoder which is used to free up the previously allocated structure
Decoder.c
ssize_t decode(void* const buffer_in, const size_t buffer_in_size,void** structure, void** root_structure) {
Asn1_structure_t* asn1_structure;
if( (*structure) != NULL )
{
fprintf(stderr, "Error");
(*structure) = NULL;
}
asn_dec_rval_t rval;
asn1_structure= 0;
rval = ber_decode(0, &asn_TYPE_descriptor_t,
(void **)&asn1_structure, buffer_in, buffer_in_size);
if(rval.code == RC_OK)
{
*structure = &(asn1_structure->choice.content);
root_structure = &(asn1_structure);
return rval.consumed;
}
else {
if(asn1_structure!= 0){
asn_TYPE_descriptor_t.free_struct(&asn_TYPE_descriptor_t, asn1_structure, 0);
}
perror("Error during decoding \n");
return -1;
}
return -1;
}
void clean_decoder_instances(void** root_structure){
if(asn_fprint(stdout,&asn_TYPE_descriptor_t, (Asn1_structure_t*)(*root_structure)) == -1){
fprintf(stderr,"Error printing structure \n");
}
else{
asn_TYPE_descriptor_t.free_struct(&asn_TYPE_descriptor_t,(Asn1_structure_t*)(*root_structure),0);
}
}
When the clean_decoder_instances method is called, the free (): invalid pointer error occurs and the method to print the structure gives the "absent" output. How should I handle out-of-scope structure clean?