IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Opened 19 years ago

Closed 19 years ago

#845 closed defect (worksforme)

psHashAdd stomps on memory

Reported by: eugene Owned by: Paul Price
Priority: high Milestone:
Component: astro Version: 0.12.99
Severity: normal Keywords:
Cc:

Description

psHashAdd is supposed to free the existing item of the same name, but does not:

psHash *hash = psHashAlloc(16);

psHashAdd (hash, "name", item);

psMemId id = psMemGetId();
psHashAdd (hash, "name", item);
psMemCheckLeaks (id, NULL, NULL, false);

this will claim a leak exists.

Change History (1)

comment:1 by Paul Price, 19 years ago

Resolution: worksforme
Status: newclosed

The code will rightly report that a leak exists in the snippet, since the
references haven't been dropped at the top level. An inspection of the psHash
code reveals the correct behaviour (free the existing pointer before adding the
new one). When the references are dropped by the user, there are no memory leaks.

#include <stdio.h>
#include "pslib.h"
#include "pstap.h"

#define SIZE 128

int main(int argc, char *argv[])
{

plan_tests(5);

char *stuff1 = psAlloc(SIZE); Stuff to put on hash
char *stuff2 = psAlloc(SIZE);
Stuff to put on hash

psHash *hash = psHashAlloc(16); Hash to test
ok(hash, "Hash allocated");

bool status1 = psHashAdd(hash, "stuff", stuff1);
ok(status1, "Added 1 to hash");
psFree(stuff1); Drop reference

psMemId last = psMemGetId(); Last memory I.D.
bool status2 = psHashAdd(hash, "stuff", stuff2);
ok(status2, "Added 2 to hash");
psFree(stuff2);
Drop reference

int numLeaks = psMemCheckLeaks(last, NULL, NULL, false); Number of leaks
ok(numLeaks == 0, "No leaks.");

psFree(hash);

done();

}

price@mithrandir:/home/mithrandir/price/mhpcc/psLib/test/types>./tap_psHash_845 1..5
ok 1 - Hash allocated
ok 2 - Added 1 to hash
ok 3 - Added 2 to hash
ok 4 - No leaks.
ok 5 - Memory Leaks

Note: See TracTickets for help on using tickets.