Memory leak in this C code


Linux-SWAT

Forum Addict!
Joined
Feb 13, 2010
Messages
9,188
Hi,

I'm supposed to have a memory leak (clang + -fsanitize=address) in this portion of code. I tried various free(&*) without success.

pthread_t tid[2];
pthread_mutex_t lock;

struct spectacle {
char spectacle_nom[20];
unsigned short spectacle_places_disponibles;
};

struct msgbuf {
long mtype;
char mtext[MAXSIZE];
};

struct message_msgbuf {
long mtype;
struct requete_info {
char nom[MAXSIZE];
int msqid_demandeur;
int spectacle;
int nb_places_demandees;
} info;
};

sem_t * tsemaphore;
int tshmid_spectacles;
int tmsqid;
tmsqid_demandeur;
tspectacle;
tnb_places_demandees;

void Fils_Reservation(sem_t * semaphore, int shmid_spectacles, int msqid_demandeur, int spectacle, int nb_places_demandees){
printf("fils réserve\n");
sem_wait (semaphore);
void *addr = Attachement_SHM(shmid_spectacles);
struct spectacle *shared_spectacles = (struct spectacle*)addr;

if ((shared_spectacles[spectacle].spectacle_places_disponibles > 0) && (shared_spectacles[spectacle].spectacle_places_disponibles - nb_places_demandees >= 0)) {
shared_spectacles[spectacle].spectacle_places_disponibles = shared_spectacles[spectacle].spectacle_places_disponibles - nb_places_demandees;
printf("fils réserve OK\n");
Envoi_Entier_Mqueue(msqid_demandeur, 2, 1);
}
else {
printf("fils réserve NOK\n");
Envoi_Entier_Mqueue(msqid_demandeur, 2, 0);
}
sem_post (semaphore);
Detachement_SHM(addr);
exit(0);
}
 
Nothing stands out to me from this code, since I can't see that it is doing any allocations directly. Maybe there's something happening in the functions it calls? Especially if they're being inlined.

Could you post the complete output of the leak detection?
 
==10785==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 8 byte(s) in 2 object(s) allocated from:
#0 0x4aec1b in __interceptor_malloc /tmp/llvm-3.8.0.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3
#1 0x7f9ccf3b66c7 in _IO_vasprintf (/lib64/libc.so.6+0x766c7)

SUMMARY: AddressSanitizer: 8 byte(s) leaked in 2 allocation(s).

AFAIK I should see more info in this output, but it's not the case...
 
I guess your allocation/deallocation happens in Attachement_SHM/Detachement_SHM. What exactly do these do?
I must admit, I am way more into C++ than C and maybe this is why I cannot understand why your function Fils_Reservation calls exit(0), which I thought, would terminate your application rather than just return. Can you explain?
All this SHM-Stuff sounds a bit like shared memory utilisation. On the other hand you use pthreads and a mutex (which appears to be never used). So, what kind of parellel processing do you want to achieve? Multi-Process or multi-threading?
What does your main actually do? How is Fils_Reservation called?
 
Last edited:
There's a "database" of spectacles (shows) in the SHM.
The ticket buying system is in both a processus and a thread (it's a school project), and mounts the SHM, and decrease the seat number (when the semaphore is free, I probably forgot to remove a mutex attempt/copy_paste), then unmount the SHM.
That's why it's just a return, processes and threads are supposed to be called many times. They are called on demand.
 
Back
Top