Board index » cppbuilder » Memory allocation/Freeing
Paul Ingram
![]() CBuilder Developer |
Memory allocation/Freeing2003-10-07 09:46:01 PM cppbuilder78 Hello all, I have been integrating some old 'C' code with a C++ wrapper. The routines build nested dynamically allocated lists of structures which are then queried for the fastest possible (well, by my hand anyway) performance. This is ancient code ported from *ix. Naturally it is necessary to refresh these caches from time to time. Memory used to be allocated/re-allocated/ free'd using calloc/realloc and free. I noticed that the Windows task manager shows that no memory is free'd when I execute a list refresh. As a result, I decided to port the 'C' to C++ in tbe simplest possible manner, use new and delete and reap the benefits of exception handling etc., I have completed the 'port' and it does not seem to me that any memory is being free'd. The routines to carry out the loading and freeing are all hand crafted and resemble like the following... Sorry about all the __LINE__ debug, I have been digging. Structure definition... typedef struct _TariffDefClass { Node Node ; /* Name is tariff name, Id is TariffDef database Id */ int TariffNameId ; double Cost ; } TariffDefClass ; Allocation looks like... { TariffDefClass *ac ; //... ac = new TariffDefClass ; memset(ac, 0, sizeof(TariffDefClass *)) ; ac->Node.Name= StrNew(lp->Rate) ; // This is tariff_name ac->Node.Id = lp->TariffDefinitionId ; ac->TariffNameId = lp->RateId ; // This is tariff_name_id ac->Cost = lp->Cost ; // Contd... } void DelTariffDefClass( TariffDefClass **ac ) { int LastLine = __LINE__ ; if ( ! *ac ) return ; try { StrDispose(((*ac)->Node.Name)) ;/* Give back name memory to kernel*/ LastLine = __LINE__ ; delete *ac ; LastLine = __LINE__ ; } catch (Exception &exception) { history("%s, %s[%d]: %s, LastLine=%d", Id, __FUNC__, __LINE__, exception.Message, LastLine) ; throw ; } } I have tried using... HeapCompact(GetProcessHeap(), 0) ; hoping that garbage collection would take place but to no avail. Each refresh takes about 200Mb ATM so it doesn't take too many refreshes to bring the server to it's knees. Before I go ahead with an idea of allocating my own heap, managing it and dropping it when I refresh, do you have any ideas what I might be doing wrong? do you have any ideas what I might do to tidy up more effectively? Any other (constructive) ideas? Best Regards, Paul. |