Operating System - HP-UX
1751957 Members
5271 Online
108783 Solutions
New Discussion юеВ

Please advise where is the memory leak in my pro*c

 
vj_agl
New Member

Please advise where is the memory leak in my pro*c

void DBUser::LoadCollection(CollectionBase& collection)
{

EXEC SQL BEGIN DECLARE SECTION;
struct sqlca sqlca;
EXEC SQL END DECLARE SECTION;

memset(&sqlca, 0, sizeof(sqlca));

DmoUser *user = NULL;

//----------------------------------------------------
// Thread Based Arrays to Support Multithreaded Mode
//----------------------------------------------------
EXEC SQL BEGIN DECLARE SECTION;
struct UserRec *user_rec_ptr;
struct UserInd *user_ind_ptr;
EXEC SQL END DECLARE SECTION;

UserRec& user_rec = userRec[pthread_self()];
user_rec_ptr = &user_rec;

UserInd& user_ind = userInd[pthread_self()];
user_ind_ptr = &user_ind;

EXEC SQL WHENEVER SQLERROR DO SqlError( "DBUser::LoadCollection() error\n", sqlca);

//--------------------------
// Get a SQL Context
//--------------------------
TRACELOG( TRACE_SYS_DB, "DBUser::LoadCollection Getting context");
sql_context *ctx;
ctx = DBMultiThreadSingleton::GetThreadContext();
EXEC SQL CONTEXT USE :*ctx;

//--------------------------------------
// Declare and allocate cursor variable.
//--------------------------------------
DBCursor dbCursor;
EXEC SQL BEGIN DECLARE SECTION;
SQL_CURSOR *user_cv = &dbCursor;
EXEC SQL END DECLARE SECTION;

//---------------------------------
// Open users cursor
//---------------------------------
EXEC SQL EXECUTE
BEGIN
users_pkg.get_users(:user_cv);
END;
END-EXEC;

//---------------------------------
// Fetch and process user_cv rows
// Close user_cv cursor
//---------------------------------
try
{
// Break when no more rows
EXEC SQL WHENEVER NOT FOUND DO break;

for (;;)
{
EXEC SQL FETCH :user_cv
INTO :user_rec_ptr INDICATOR :user_ind_ptr;

user = new DmoUser;

//----------------------
// Populate data members
//----------------------
user->SetUserID(user_rec.user_id);
user->SetLoginID(VARCHAR_TO_STR(user_rec.user_login_id));
user->SetSecurityProfilePk( IntVal(user_rec.security_profile_pk,
user_ind.security_profile_pk_ind) );
user->SetUserName(VARCHAR_TO_STR(user_rec.user_name));
user->SetSPID(VARCHAR_TO_STR(user_rec.user_group));

StrToTime(user->_userCreationTS, user_rec.user_creation_ts,
user_ind.user_creation_ts_ind);
StrToTime(user->_lastLoginTS, user_rec.last_logged_in_ts,
user_ind.last_logged_in_ts_ind);
user->SetCreatedUserLoginId(VARCHAR_TO_STR(user_rec.created_user_login_id));
user->SetNotes(VARCHAR_TO_STR(user_rec.notes));

//----------------------------------------
// Add fetched User to list
//----------------------------------------
collection.Add(user);
user = NULL;

}
EXEC SQL WHENEVER NOT FOUND CONTINUE;

EXEC SQL CLOSE :user_cv;
}
catch(...)
{
EXEC SQL CLOSE :user_cv;
throw;
}

}
4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: Please advise where is the memory leak in my pro*c

There are no obvious C or C++ allocations.
You could use gdb's leak detection tools.
Steven E. Protter
Exalted Contributor

Re: Please advise where is the memory leak in my pro*c

Shalom,

Memory leak detector.

http://www.hpux.ws/?p=8

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Roland Piette
Regular Advisor

Re: Please advise where is the memory leak in my pro*c

Hello,

In the try function or method you have an endless loop in wich you allocate memory ressource with the new operator :

user = new DmoUser;

You cannot release these ressource with an assign of NLL

user = NULL;

You hace to use de destructor delete instead ! So replace user = NULL by delete user !

Regards,
Roland

Roland Piette
Regular Advisor

Re: Please advise where is the memory leak in my pro*c

Hi again,

Additionnal info :

'new' and 'delete' are two operators used in C++ (object oriented program) to allocate objects in the heap memory. So when you create an instance with 'new' operator an amount of memory is reserve for the object. When you assign a NULL to this pointer (user variable) the previous allocated object can any more be released. This part of memory is hold by the process and will be released when it finishes or crashes. So to release the object you have to use the 'delete' operator !

Regards,
Roland