Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ SQL » PostgreSQL Session.Open Leak??
PostgreSQL Session.Open Leak?? [message #14240] Wed, 20 February 2008 19:52 Go to next message
indiocolifa is currently offline  indiocolifa
Messages: 49
Registered: January 2008
Location: Argentina
Member
Anyone tried PostgreSQL/U++? I'm getting leaks detected at the end of program execution, and I've discovered it goes away when isolating the pgsql Session open function in my program.

Note that I'm mantaining a global PostgreSQL session variable in a singleton class (this member variable is not static). Getting the class instance and getting a pointer to that PostgreSQLSession variable works well in my program...

Ideas? Very Happy
Re: PostgreSQL Session.Open Leak?? [message #14246 is a reply to message #14240] Wed, 20 February 2008 22:30 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 697
Registered: December 2005
Location: Budapest, Hungary
Contributor
testcase?
Re: PostgreSQL Session.Open Leak?? [message #14250 is a reply to message #14240] Wed, 20 February 2008 23:30 Go to previous messageGo to next message
indiocolifa is currently offline  indiocolifa
Messages: 49
Registered: January 2008
Location: Argentina
Member
Tomorrow I'll post the code, it's on the machine at my job running 2003 Server SP1+PostgreSQL 8.3+UPP 2008.1.
Re: PostgreSQL Session.Open Leak?? [message #14276 is a reply to message #14240] Thu, 21 February 2008 15:35 Go to previous messageGo to next message
indiocolifa is currently offline  indiocolifa
Messages: 49
Registered: January 2008
Location: Argentina
Member
MAIN.CPP

include "sr2k8.h"
#include "MainWindow.h"

#define SCHEMADIALECT <PostgreSQL/PostgreSQLSchema.h>
#include <Sql/sch_source.h>

bool ConnectToPSQL();
void LoadConfig();

GUI_APP_MAIN
{
	::SetLanguage( ::GetSystemLNG() );
	
	LoadConfig();
	if (ConnectToPSQL())
	{
		MainWindow mw;
		mw.Run();
	}	
}

void LoadConfig()
{
	String cfgfile = ConfigFile();
	if (FileExists(cfgfile))
	{
		VectorMap<String, String> cfg = LoadIniFile(cfgfile);
	}
	else
	{
		// configuracion por default
		String s;
		s << "APPV=1.0\n"
		  << "HOST=10.62.200.1\n"
		  << "DATABASE=S2K8\n"
		  << "USER=sis\n"
		  << "PASS=sis\n";
		
		if (!SaveFile(ConfigFile(),s))
		{
			Exclamation("Error grabando archivo de configuración");
			exit(-1);
		}
			
	}
}

bool ConnectToPSQL()
{
	if (!G_STATE->GetPsqlSession()->Open("host=localhost user=sis password=sis dbname=S2K8"))
	{							
		Exclamation(Format("Error abriendo base de datos: %s", 
			DeQtf(G_STATE->GetPsqlSession()->GetLastError())));
		return false;
	}
	
	return true;
}


Sr2k8.H

#ifndef _sr2k8_sr2k8_h
#define _sr2k8_sr2k8_h

#include <CtrlLib/CtrlLib.h>
#include <SqlCtrl/SqlCtrl.h>

using namespace Upp;

// definiciones para PostgreSQL
#include <PostgreSQL/PostgreSQL.h>
#define SCHEMADIALECT <PostgreSQL/PostgreSQLSchema.h>
#define MODEL <sr2k8/sr2k8db.sch>
#include <Sql/sch_header.h>

#include "globalState.h"
#define G_STATE GlobalState::getInst()

// layout de las ventanas
#define LAYOUTFILE <sr2k8/sr2k8.lay>
#include <CtrlCore/lay.h>

#endif


globalState.h (singleton class spec)

#ifndef _sr2k8_globalState_h_
#define _sr2k8_globalState_h_

#include <PostgreSQL/PostgreSQL.h>

using namespace Upp;

// clase singleton que mantiene un estado global del sistema

class GlobalState
{
	static GlobalState* pInstance;
	
	PostgreSQLSession 	pgsql;
	String				currentUser;
	
	public:
	static GlobalState* getInst()
	{
		if (!pInstance)
			pInstance = new GlobalState;
		
		return pInstance;				
	}
	
	PostgreSQLSession* GetPsqlSession() { return &pgsql; }
			
	protected:
	GlobalState() {
		currentUser == "";		
	};
	GlobalState(const GlobalState&);
	GlobalState& operator= (const GlobalState&);
};

#endif


This is initialized in globalstate.cpp as follows:

#include "globalState.h"

GlobalState* GlobalState::pInstance = NULL;


Do you see any problem related to the singleton??? Maybe it's that 'new Globalstate' on singleton getInst() method.

[Updated on: Thu, 21 February 2008 15:40]

Report message to a moderator

Re: PostgreSQL Session.Open Leak?? [message #14278 is a reply to message #14240] Thu, 21 February 2008 15:49 Go to previous messageGo to next message
indiocolifa is currently offline  indiocolifa
Messages: 49
Registered: January 2008
Location: Argentina
Member
Yesss,,,, fixed it!

The last line of main is now:

delete G_STATE;


G_STATE is a macro so, it's equal to:

delete globalState::getInst();


since getInst returns a pointer, it's all ok.
Re: PostgreSQL Session.Open Leak?? [message #14281 is a reply to message #14278] Thu, 21 February 2008 16:14 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
indiocolifa wrote on Thu, 21 February 2008 09:49

Yesss,,,, fixed it!

The last line of main is now:

delete G_STATE;


G_STATE is a macro so, it's equal to:

delete globalState::getInst();


since getInst returns a pointer, it's all ok.


Well, well, that is the price of NOT using U++ way od programming:)

We have nice Single template.

Remove these statics and just use Single<GlobalState>() instead of GlobalState::getInst. Also, your version is MT broken (depends on compiler); Single is guaranteed to work.

No well-behaved program should contain similar "delete" Smile

Mirek
Re: PostgreSQL Session.Open Leak?? [message #14282 is a reply to message #14240] Thu, 21 February 2008 16:26 Go to previous messageGo to next message
indiocolifa is currently offline  indiocolifa
Messages: 49
Registered: January 2008
Location: Argentina
Member
Fixed!
Re: PostgreSQL Session.Open Leak?? [message #14283 is a reply to message #14281] Thu, 21 February 2008 16:28 Go to previous messageGo to next message
indiocolifa is currently offline  indiocolifa
Messages: 49
Registered: January 2008
Location: Argentina
Member
luzr wrote on Thu, 21 February 2008 13:14

indiocolifa wrote on Thu, 21 February 2008 09:49

Yesss,,,, fixed it!

The last line of main is now:

delete G_STATE;


G_STATE is a macro so, it's equal to:

delete globalState::getInst();


since getInst returns a pointer, it's all ok.


Well, well, that is the price of NOT using U++ way od programming:)

We have nice Single template.

Remove these statics and just use Single<GlobalState>() instead of GlobalState::getInst. Also, your version is MT broken (depends on compiler); Single is guaranteed to work.

No well-behaved program should contain similar "delete" Smile

Mirek



Hey Mirek, great!

Should I define Globalstate as a normal class (I mean, not using statics like in the Singleton pattern)?

Discovering U++ is fun!
Re: PostgreSQL Session.Open Leak?? [message #14305 is a reply to message #14283] Fri, 22 February 2008 10:09 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
indiocolifa wrote on Thu, 21 February 2008 10:28


Should I define Globalstate as a normal class (I mean, not using statics like in the Singleton pattern)?

Discovering U++ is fun!



Depends. I would either use Single directly or created a global function to return Single:

GlobalState& TheGlobalState() {
   return Single<GlobalState>();
}


but you can also stay a bit closer to "exact" singleton pattern:

class GlobalState {
   static GlobalState& getInst() { return Single<GlobalState>(); }
};


or if you insist on pointer (which IMO is stupid here, as the result can never be NULL):

class GlobalState {
   static GlobalState *getInst() { return &Single<GlobalState>(); }
};


In either case, GlobalState will be destructed at exit, avoiding memory leak.

Mirek
Previous Topic: updating/discarding table data on dialogs...
Next Topic: Can't fetch row...
Goto Forum:
  


Current Time: Mon Apr 29 01:13:42 CEST 2024

Total time taken to generate the page: 0.04696 seconds