我有这个代码,它使用OCIlib:
int HoleInstrumentenDiffListeDB(GTree *tree)
{
OCI_Connection* cn;
OCI_Statement* st;
OCI_Resultset* rs;
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
return EXIT_FAILURE;
char query[500];
query[0] ='\0';
cn = OCI_ConnectionCreate( "db", "user", "pass", OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);
strcat(query, "SELECT...");
OCI_ExecuteStmt(st, query);
rs = OCI_GetResultset(st);
int i = 1;
int j = 0;
char *symbolp;
while (OCI_FetchNext(rs)){
const char * symbolp = OCI_GetString(rs,2);
switch ( * OCI_GetString(rs,3))
{
case 'N':
insertQot(tree, symbolp, OCI_GetInt(rs, 1) );
printf("new \n");
break;
case 'U':
insertQot(tree, symbolp, OCI_GetInt(rs, 1) );
printf("upd \n");
break;
case 'D':
deleteQot(tree, symbolp);
printf("del \n");
break;
}
}
OCI_Cleanup();
return 1;
}
建立连接,选择似乎工作。但是在用valgrind检查时我会遇到很多错误。这里只有几个:
==21085== Source and destination overlap in memcpy(0x742dc80, 0x742dc80, 1)
==21085== at 0x4A24F66: _intel_fast_memcpy (mc_replace_strmem.c:894)
==21085== by 0x562E6D7: kpufprow (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x562BFDE: kpufch0 (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x562ACB6: kpufch (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5520A2F: OCIStmtFetch2 (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x4CDAF6E: OCI_FetchData (resultset.c:506)
==21085== by 0x4CDB405: OCI_FetchNext (resultset.c:1117)
==21085== by 0x401B95: HoleInstrumentenDiffListeDB (unzipper_m.c:221)
==21085== by 0x402AA9: main (unzipper_m.c:691)
==21085== Conditional jump or move depends on uninitialised value(s)
==21085== at 0x5EC79DF: slpmloclfv (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5EC771E: slpmloc (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5EC4C44: lpmloadpkg (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5EAAA8E: lfvLoadPkg (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5EAA719: lfvSetShlMode (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5EAA518: lfvini1 (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5EAA234: lfvinit (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5ACD1C9: kpummpin (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x547EED8: kpuenvcr (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x551FCD5: OCIEnvCreate (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x4CE311B: OCI_Initialize (library.c:1140)
==21085== by 0x401942: HoleInstrumentenDiffListeDB (unzipper_m.c:207)
==21085== Use of uninitialised value of size 8
==21085== at 0x56DFE05: ztceadecbk (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x56DC6BC: ztceb_decblk (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x56DC3BE: ztcebf (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x56DBB8A: ztcef (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x56DBC6E: ztcedec (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x57341A3: ztvo5ed (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x5734FA5: ztvo5ver (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x54C899D: kpu8lgn (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x54C649C: kpuauthxa (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x54C5EA5: kpuauth (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x551FF1A: OCISessionBegin (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085== by 0x4CD45EA: OCI_ConnectionLogon (connection.c:598)
我可以改进我的代码吗?如果不是 - 离开它有多安全?
答案 0 :(得分:0)
首先,你应该为每个应用程序调用一次OCI_Initialize()和OCI_Cleanup(),最好是从主线程调用。
关于Valgrind跟踪,您可以看到它涉及Oracle提供的Oracle客户端共享库的内部代码。对于使用纯OCI代码编码的应用程序,您将获得相同的结果。这些跟踪与构建在最高OCI上的OCILIB代码无关。
创建一个只调用OCI调用OCIEnvCreate()的应用程序并查看Valgrind跟踪。你会有类似的痕迹。
此致
文森特