problem with TBlob*

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
banita
Posts: 29
Joined: Fri 19 Jun 2009 14:31

problem with TBlob*

Post by banita » Fri 14 Aug 2009 12:42

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

banita
Posts: 29
Joined: Fri 19 Jun 2009 14:31

Post by banita » Fri 14 Aug 2009 13:43

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?

eduardosic
Posts: 387
Joined: Fri 18 Nov 2005 00:26
Location: Brazil

Post by eduardosic » Fri 14 Aug 2009 14:03

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

banita
Posts: 29
Joined: Fri 19 Jun 2009 14:31

Re: problem with TBlob*

Post by banita » Fri 14 Aug 2009 22:32

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.

Plash
Devart Team
Posts: 2844
Joined: Wed 10 May 2006 07:09

Post by Plash » Mon 17 Aug 2009 07:07

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.

banita
Posts: 29
Joined: Fri 19 Jun 2009 14:31

Post by banita » Mon 17 Aug 2009 13:31

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;

Post Reply