Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

about delete ImaqBuffer

Hello,
My card is NI-IMAQ PCI-1405, and I am working in VC++2005.
 
When I SNAP a picture, there are data in "ImaqBuffer" pointer as I sent to the the function "imgSnap (Sid, (void **)&ImaqBuffer)".
I get the data and do some image processing, for example, flip image left to right, and I save the processed image in "temp" pointer.
and I want to show the processed image, so I let ImaqBuffer=temp, and delete the memory of old ImaqBuffer points to, there comes an error.
I dont know why I cant delete the useless memory?
 
here is the the part of my code:
 
void CIMGsnapDlg::flipLR(){
 
 // Have the size as same as image in ImaqBuffer
 char* temp;
 temp= new char[AcqWinHeight*AcqWinWidth]; 
 
 /***flip the image and save in temp***/
 int index1;
 int index2;
 for(int i=0;i<AcqWinHeight;i++){
   for(int j=0;j<AcqWinWidth;j++){
     index1=i*AcqWinWidth+j;
     index2=i*AcqWinWidth+(AcqWinWidth-j-1);
     temp[index1]=*(ImaqBuffer+index2);
   }
 }
 /************************************/
 
char* temp2=ImaqBuffer; // point to old memory of ImaqBuffer
ImaqBuffer=temp;
temp=NULL;
delete [] temp2;            // delete the old memory of ImaqBuffer and here is the ERROR
OnPaint();  // show the processed image
}
 
Since the Function Reference Help about function: "rval imgGrab(SESSION_ID sid, void** bufAddr, uInt32 waitForNext)" mentions that if
bufAddr points to a NULL pointer, this call allocates a buffer, acquires an image, and returns the buffer address in the location specified by bufAddr.
the "ImaqBuffer" in my code is points to NULL before I sent to the function.
Is this anything connection about my error?
Thanks for help.
0 Kudos
Message 1 of 6
(4,137 Views)

Hello ccmkn,

   若想要將image pointer做釋放的動作,您可以使用imaqdispose()

   另外我看一下您的程式碼,您這樣的寫法可能會有問題,因為在imaqSnap(SESSION_ID sessionID, Image* image, Rect rect)中

   image的資料型態和char是不ㄧ樣的,所以您的處理方法會有問題

   針對您的問題,建議您:

   (a) 參考範例程式的寫法: C:\Documents and Settings\All Users\Documents\National Instruments\NI-IMAQ\Examples\MSVC

   (b) 若要做flip的動作,可以使用imaqflip()

            int imaqFlip(Image* dest, const Image* source, FlipAxis axis);

       Purpose

            Flips an image over an axis.

 

    Alexander Chen, AE NITW

0 Kudos
Message 2 of 6
(4,072 Views)
Thanks for your response, Alexander Chen

我在Function Reference Help about function這個文件檔中,只找到了imaqdispose()的功能,並無imaq開頭的函式,但卻也找不到有類似imaqflip()函式的說明,不知道是否我忽略掉了什麼。

我會再研究看看imaqdispose()功能,畢竟以前學過C語言,總是覺得要把自己產生的記憶體空間釋放掉心裡才會踏實一點,很感謝你的提示。

而因為我是要做後續影像處理的工作,所以這邊我只先寫了一個flipLR當作測試,我是想找到指標存放影像的位置和資料,而在程式給的範例中,例如HLsnap.c這檔案,他存放影像的指標宣告如下:
static Int8*        ImaqBuffer=NULL;

其中Int8 在niimaq.h中的定義為char:

#ifndef _NI_Int8_DEFINED_
    #define _NI_Int8_DEFINED_
    typedef char                Int8;
#endif

所以我才大膽的用自己新建的char陣列去存這資料,並留給我以後影像處理使用,而因為我找到的imgSnap()的函式定義和你的imaqSnap()不太相同:
rval imgSnap(SESSION_ID sid, void** bufAddr);

所以我不太清楚你的image的資料型態是指什麼?

不知道是否我又少漏看了什麼資料,還是我忽略了什麼細節。
我會再嘗試找看看你所提供的函式。

謝謝。

0 Kudos
Message 3 of 6
(4,059 Views)

Hello ccmkn,

   抱歉我看錯了你使用的函式,先前誤以為你是使用IMAQ Vision中的imaqsnap()
   您的程式寫法是正確的,不曉得您遇到的錯誤訊息是什麼呢?
   如果方便的話,是否能請您將整個專案檔案post給我們參考一下
   讓我們試試看會有什麼樣的錯誤訊息

   Alexander Chen, AE NITW

0 Kudos
Message 4 of 6
(4,026 Views)
Hello, Alexander Chen

感謝你熱心的回覆和幫忙,希望不會對你造成莫大的困擾。

我想我把問題簡單化好了,
我把我程式中的測試的部分給註解起來,就留下當要關閉程式時會發生的錯誤,也就是當程式呼叫這個函式時:
void CIMGsnapDlg:: OnBnClickedCancel()
{
    // TODO: 在此加入控制項告知處理常式程式碼

    if(Sid)
        imgClose (Sid, TRUE);
    if(Iid)
        imgClose (Iid, TRUE);
    if(ImaqTemp!=NULL)
        {delete [] ImaqTemp;}

/***** THERE IS THE PROBLEM *****/
    if(ImaqBuffer!=NULL)
        {delete [] ImaqBuffer;}
/********************************/

    OnCancel();
}

就是當要釋放ImaqBuffer這個指標所指向的記憶體空間時所發生的錯誤。

檔案中我附上了錯誤的訊息畫面跟我整個專案檔還有簡單提一下這程式的說明文字檔。
http://140.116.5.200/~e9493122/PROBLEM.zip
我會再繼續嘗試一下你說的imaqdispose()函式,我覺得這或許可以解決我的問題。

總之,再次感謝你。



由 ccmkn 在 06-21-2008 09:09 AM 上編輯的訊息

由 ccmkn 在 06-21-2008 09:09 AM 上編輯的訊息
0 Kudos
Message 5 of 6
(4,021 Views)
不好意思,我前面的文章似乎有打錯,在NI-IMAQ Function Reference Help的文件中,我找到的函式是:

rval imgDisposeBuffer(void* buffPtrAddr);

並不是imaqdispose()


怕會造成其他人的困惑,先回文以示修正。


謝謝



由 ccmkn 在 06-22-2008 01:47 AM 上編輯的訊息
0 Kudos
Message 6 of 6
(4,001 Views)