Page 1 of 1
problem with TBlob*
Posted: Fri 14 Aug 2009 12:42
by banita
postgresql 8.4, unidac 2.70.0.9, c++builder 2007
I try add bitmap files into foto field. I use TUniSQL.
Under OnClick event I write:
Code: Select all
unsql1->SQL->Clear();
unsql1->SQL->Add("INSERT INTO t1(foto) VALUES(:P)");
TBlob* blob = new TBlob(false);
blob->LoadFromFile("E:\b.bmp");
unsql1->ParamByName("P")->AsBlobRef = blob;
unsql1->Execute();
delete blob;
When I press button the bitmap is added to table t1 but when I close application I get error:
"invalid pointer operation"
When I comment delete blob; everythink works fine.
Whot I do wrong?
I think this is a reason:
unsql1->ParamByName("P")->AsBlobRef = blob;
..
delete blob;
but how I can delete blob object?
sory for my english
Posted: Fri 14 Aug 2009 13:43
by banita
I try do this with firebird:
Code: Select all
unsql1->SQL->Clear();
unsql1->SQL->Add("INSERT INTO table04 VALUES(GEN_ID(GEN_TABLE04_ID,1),:P)");
TBlob* b = new TBlob(false);
b->LoadFromFile("E:\b.bmp");
unsql1->ParamByName("P")->AsBlobRef = b;
unsql1->Execute();
delete b;
and here: unsql1->Execute(); I get error
"acces violation... in modeule dac105.bpl"
when I do this like that:
Code: Select all
unsql1->SQL->Clear();
unsql1->SQL->Add("INSERT INTO table04 VALUES(GEN_ID(GEN_TABLE04_ID,1),:P)");
unsql1->ParamByName("P")->AsBlobRef->LoadFromFile("E:\b.bmp");
unsql1->Execute();
all works fine. why?
Posted: Fri 14 Aug 2009 14:03
by eduardosic
banita wrote:I try do this with firebird:
Code: Select all
unsql1->SQL->Clear();
unsql1->SQL->Add("INSERT INTO table04 VALUES(GEN_ID(GEN_TABLE04_ID,1),:P)");
TBlob* b = new TBlob(false);
b->LoadFromFile("E:\b.bmp");
unsql1->ParamByName("P")->AsBlobRef = b;
unsql1->Execute();
delete b;
and here: unsql1->Execute(); I get error
"acces violation... in modeule dac105.bpl"
when I do this like that:
Code: Select all
unsql1->SQL->Clear();
unsql1->SQL->Add("INSERT INTO table04 VALUES(GEN_ID(GEN_TABLE04_ID,1),:P)");
unsql1->ParamByName("P")->AsBlobRef->LoadFromFile("E:\b.bmp");
unsql1->Execute();
all works fine. why?
In Delphi i make this..
Code: Select all
unsql1.SQL.Clear;
unsql1.SQL.Add( 'INSERT INTO table04 VALUES(GEN_ID(GEN_TABLE04_ID,1),:P)');
TBlobField(unsql1.ParamByName( 'P')).LoadFromFile( 'E:\b.bmp', ftBlob );
unsql1.Execute;
work's fine
Re: problem with TBlob*
Posted: Fri 14 Aug 2009 22:32
by banita
banita wrote:
Code: Select all
unsql1->SQL->Clear();
unsql1->SQL->Add("INSERT INTO t1(foto) VALUES(:P)");
TBlob* blob = new TBlob(false);
blob->LoadFromFile("E:\b.bmp");
unsql1->ParamByName("P")->AsBlobRef = blob;
unsql1->Execute();
delete blob;
When I write before 'delete blob' this code:
unsql1->Params->Clear();
then all works fine but it is strange for me.
Posted: Mon 17 Aug 2009 07:07
by Plash
You should not delete a TBlob object because the parameter has a reference to it. Instead call the Release or Free method. TBlob has the reference counter. The Release method decrements this counter. UniDAC will automatically delete Blob when the reference count is zero.
Posted: Mon 17 Aug 2009 13:31
by banita
Thanks!
I think there are two ways.
first:
Code: Select all
TUniQuery* q = new TUniQuery(0);
q->Connection = con1;
q->SQL->Text = "INSERT INTO t1(foto) VALUES(:P)";
TBlob* param = new TBlob(false);
param->LoadFromFile("E:\b.bmp");
q->ParamByName("P")->AsBlobRef = param;
q->Execute();
q->Params->Clear(); // clear params
delete param; // and now can delete
delete q;
second:
Code: Select all
TUniQuery* q = new TUniQuery(0);
q->Connection = con1;
q->SQL->Text = "INSERT INTO t1(foto) VALUES(:P)";
TBlob* param = new TBlob(false);
param->LoadFromFile("E:\b.bmp");
q->ParamByName("P")->AsBlobRef = param;
q->Execute();
param->Release(); // release but dont delete
delete q;