Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing variable outside scope of a callback c++
    primarykey
    data
    text
    <p>I have been beating my head around this issue of static versus non-static, callback functions, function pointers, etc... My goal is to access data of a struct outside the scope of my callback interface. I am trying to do this within my class called TextDetect. I thought I was on track when I asked this question: <a href="https://stackoverflow.com/questions/11513071/avoiding-a-static-member-function-in-c-when-using-a-callback-interface-from-c">Avoiding a static member function in c++ when using a callback interface from C</a> However, I still can't access the data without losing scope over the data that I am most interested. At runtime, I get "Access violation reading location ..." I'll point it out below where it fails. I implemented the answer to my previous question as the following class, shown entirely (Note: vtrInitialize is part of a 3rd party api code <strong>int vtrInitialize(const char *inifile, vtrCallback cb, void *calldata);</strong>):</p> <pre><code> class TextDetect { const char * inifile; vtrImage *vtrimage; int framecount; public: TextDetect(); ~TextDetect(); void vtrCB(vtrTextTrack *track); static void vtrCB_thunk(vtrTextTrack *track, void *calldata); int vtrTest(cv::Mat); bool DrawBox(cv::Mat&amp;); vtrTextTrack *texttrack; }; TextDetect::TextDetect() : inifile("vtr.ini") { if (vtrInitialize(inifile, vtrCB_thunk, static_cast&lt;void *&gt;(this) ) == -1) std::cout &lt;&lt; "Error: Failure to initialize" &lt;&lt; std::endl; vtrimage = new vtrImage; } int TextDetect::vtrTest(cv::Mat imagetest) { /*store image data in an image structure*/ } void TextDetect::vtrCB(vtrTextTrack *track) { /*send data to command line from callback */ </code></pre> <p>I've tried copying the data I need a variety of ways and nothing works (this code is a continuation from above):</p> <pre><code> //texttrack = track; //texttrack = new vtrTextTrack (*track); memcpy(texttrack,track,sizeof(*track)); //vtrTextTrackFree(track); } void TextDetect::vtrCB_thunk(vtrTextTrack *track, void *calldata) { static_cast&lt;TextDetect *&gt;(calldata)-&gt;vtrCB(track); } </code></pre> <p>This is the member function were I want the data to be used. Texttrack is public member so I might need it outside my class as well (this code is a continuation from above):</p> <pre><code> bool TextDetect::DrawBox(cv::Mat&amp; tobeboxed) { </code></pre> <p>And I get the access violation error at runtime here at this line of code (this code is a continuation from above):</p> <pre><code> if (texttrack-&gt;best-&gt;ocrconf &gt; 90) { /*do some more stuff*/ } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload