Article View: borland.public.cppbuilder.database.ado
Article #4945Re: Invalid pointer operation
From: Harold Howe (TeamB)
Date: Tue, 04 Sep 2001 15:16
Date: Tue, 04 Sep 2001 15:16
57 lines
2945 bytes
2945 bytes
> void fnGetFields(TObject* objData,TFields& Fields)
> {
> try {
> Fields = ((TADODataSet*)objData)->Fields;
> } catch (...) {
> }
> }
This won't work because you are trying to assign a TFields* to a a TFields
reference. The correct syntax would be
Fields = *(((TADODataSet*)objData)->Fields);
I'm not sure this will compile either though. In general, you must always
refer to VCL objects by pointer.
Another quick comment, why does this routine take a TObject for a parameter?
Why not take a TADODataSet, or better yet, a TDataSet? Actually, I don't
understand the purpose of this function, since you could just dereference
the Fields property instead of calling your routine. I will assume that you
have a good reason.
> For:
> // tb is Private Object
> // ls is ListBox
> TFields fFields;
> TField fField;
These will cause problems because you aren't using pointers for these vcl
objects. All VCL objects must be created on the heap, not on the stack.
> for (int i = 0;i < fFields->Count;i++)
This won't compile because you are using -> to dereference fFields, but
fFields is not a pointer. The solution of course, based on my previous
comment, is to make fFields a pointer.
> TField* fnGetField(TObject* objData,char* cField)
> {
> TField* fField;
> try {
> fField = ((TADODataSet*)objData)->FieldByName(cField);
> } catch (...) {
> }
> return(fField);
> }
This code is not exception safe because you don't initialize fField to
something reasonable. If an exception occurs, the return value from this
routine is garbage. I suggest that initialize fField to 0 or NULL.
Note: the compiler errors regarding invalid pointer addition are probably
due to the for loop using -> on something that is not a pointer.
Harold Howe [TeamB]
http://www.bcbdev.com
Technical Details
Message-ID:
<3b94f036$2_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!sn-xit-01!supernews.com!feeder.qis.net!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3b906bcc_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f5ca7,99a1a2e4cbb50d24 X-Google-Attributes: gidf5ca7,public X-Google-Thread: 10698b,99a1a2e4cbb50d24 X-Google-Attributes: gid10698b,public X-Google-Thread: 103c8d,99a1a2e4cbb50d24 X-Google-Attributes: gid103c8d,public X-Google-ArrivalTime: 2001-09-04 08:22:57 PST From: "Harold Howe \(TeamB\)" <hhowe@bcbdev.com> Newsgroups: borland.public.cppbuilder.database.ado,borland.public.cppbuilder.language,borland.public.cppbuilder.winapi References: <3b906bcc_2@dnews> Subject: Re: Invalid pointer operation Date: Tue, 4 Sep 2001 10:16:36 -0500 Lines: 59 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 NNTP-Posting-Host: 12.152.193.134 Message-ID: <3b94f036$2_1@dnews> X-Trace: dnews 999616566 12.152.193.134 (4 Sep 2001 08:16:06 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!sn-xit-01!supernews.com!feeder.qis.net!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews