|
|
Home » U++ Library support » U++ SQL » PostgreSQL Session.Open Leak??
|
|
|
Re: PostgreSQL Session.Open Leak?? [message #14276 is a reply to message #14240] |
Thu, 21 February 2008 15:35   |
 |
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 #14281 is a reply to message #14278] |
Thu, 21 February 2008 16:14   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
indiocolifa wrote on Thu, 21 February 2008 09:49 | Yesss,,,, fixed it!
The last line of main is now:
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" 
Mirek
|
|
|
|
Re: PostgreSQL Session.Open Leak?? [message #14283 is a reply to message #14281] |
Thu, 21 February 2008 16:28   |
 |
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:
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" 
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!
|
|
|
|
Goto Forum:
Current Time: Sat Apr 26 14:31:32 CEST 2025
Total time taken to generate the page: 0.00652 seconds
|
|
|