在使用大字节数组写入到数据库时遇到的小问题

没有修改之前的代码:

_variant_t varBlob;

                                     varBlob.vt = VT_ARRAY|VT_UI1;

                                     SAFEARRAY *m_psafe;

                                     SAFEARRAYBOUND m_baud;

                                     m_baud.lLbound=0;

                                     m_baud.cElements= imageLen;

                                     m_psafe = SafeArrayCreate(VT_UI1,1,&m_baud);

                                     for(long i=0;i<imageLen;i++)

                                      {

                                               SafeArrayPutElement(m_psafe,&i, imageBuffer++);

                                     }

                                     varBlob.parray= m_psafe;

                                     CDataBaseOperation dbOper;

                                     dbOper.OnInitADOConn();

                                     dbOper.WriteToDataBase(imageName,varBlob);

在imageBuffer的声明:

        Char *imgeBuffer = new Char[len];

经过以上使用之后,需要释放内存空间:

       IF(imageBuffer != NULL)

           Delete [] imageBuffer;

但是程序走到这里就会报错,说明imageBuffer 的释放有问题,按理应该是正确的,开始还以为是在以上的赋值过程出现了问题,后来发现,在SafeArrayPutElement()函数中,imageBuffer指针是在移动的,也就是当执行完以上过程后,imageBuffer的指针指向了该字节串的末尾,删除肯定会报错的,那么按一下修改之后:

_variant_t varBlob;

                                     varBlob.vt = VT_ARRAY|VT_UI1;

                                     SAFEARRAY *m_psafe;

                                     SAFEARRAYBOUND m_baud;

                                     m_baud.lLbound=0;

                                     m_baud.cElements= imageLen;

                                     m_psafe = SafeArrayCreate(VT_UI1,1,&m_baud);

                                     byte * temp = imageBuffer;

                                     for(long i=0;i<imageLen;i++)

                                     {

                                               SafeArrayPutElement(m_psafe,&i,temp++);

                                     }

                                     varBlob.parray= m_psafe;

                                     CDataBaseOperation dbOper;

                                     dbOper.OnInitADOConn();

                                     dbOper.WriteToDataBase(imageName,varBlob);

                                    

其实这个问题早就发现了,释放就出错,后来干脆不释放,可是存储的数据量大啊,操作几次之后,就会报内存不足的问题,因此不能存在侥幸心理,你想让机器自己把你制造的垃圾自动清理掉的话,你就要更多的代码来管理它。。。因此还是提早解决问题吧!


你可能感兴趣的:(数据库,UI,制造,null,delete,byte)