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

Doc: Leaks en crear mensaje #69

Open
neverkas opened this issue Apr 13, 2022 · 0 comments
Open

Doc: Leaks en crear mensaje #69

neverkas opened this issue Apr 13, 2022 · 0 comments
Labels
Documentation Improvements or additions to documentation Valgrind

Comments

@neverkas
Copy link
Collaborator

  1. En paquete_create() creamos un buffer vacío con empty_buffer() y alocamos espacio para el buffer
  2. En main estamos pisando el espacio alocado en el paso anterior con un nuevo mensaje alocado en otro malloc
  3. Se generó un memory leak porque perdimos el buffer que creamos en el primer paso
t_buffer* empty_buffer() {
  t_buffer* nuevoBuffer = NULL;
  nuevoBuffer = malloc(sizeof(t_buffer));
}

t_paquete* paquete_create() {
  t_paquete* nuevo_paquete = NULL;
  nuevo_paquete = malloc(sizeof(t_paquete));

  nuevo_paquete->buffer = NULL;
  nuevo_paquete->buffer = empty_buffer();

  return nuevo_paquete;
}


t_buffer* crear_mensaje(char* texto) {
  int mensaje_longitud = strlen(texto) + 1; // sumamos el '\0' que indica fin de cadena
  int mensaje_size = sizeof(char) * mensaje_longitud;

  t_buffer* mensaje = NULL;
  mensaje = empty_buffer(); 
  mensaje->stream = malloc(mensaje_size); 
  mensaje->size = mensaje_size;

  // memcpy(destino, fuente, cantidad_bytes)
  memcpy(mensaje->stream, (void*)texto, mensaje_size);

  return mensaje;
}


void main(){
  t_paquete* paquete1 = paquete_create();
  t_buffer* mensaje1 = crear_mensaje("chau");
  paquete1->buffer = mensaje1;
}
@neverkas neverkas added Documentation Improvements or additions to documentation Valgrind labels Apr 13, 2022
@neverkas neverkas added this to Documentation in Manucproject Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Improvements or additions to documentation Valgrind
Projects
Manucproject
Documentation
Development

No branches or pull requests

1 participant