Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are mixing together two ways of allocating/freeing memory - <code>malloc/free</code> and <code>new/delete</code> :</p> <pre><code>CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments)); </code></pre> <p>and </p> <pre><code>delete moments; </code></pre> <p>You should not mix the two. <code>malloc/free</code> do not call constructor/destructor on the allocated memory. <code>new/delete</code> do call constructor/destructor on the allocated memory, and MIGHT be using other memory function instead of <code>malloc/free</code> to allocate/free the actual memory. As a result, you can get all sorts of errors emanating from reasons ranging from calling a destructor on unconstructed object to using non-corresponding functions to allocate/free memory.</p> <p>Plus, there can be other problems, of course. This is an obvious one and you should start by fixing it.</p> <p>EDIT #1: While everything said above is true and should be fixed, <a href="https://stackoverflow.com/a/18043326/917249">Andrey Smorodov's answer</a>, probably, nailed it: you unconditionally <code>return</code> from inside the loop. I should've not stopped checking your code after I saw <code>malloc/delete</code> problem ;).</p> <p>EDIT #2: To answer your comment. If <code>CvMoments</code> is a C-struct, use <code>free</code> instead of <code>delete</code>. If it is a C++ class, use <code>new</code> instead of <code>malloc</code>. </p> <p>You also have to review the code in the end of your loop. It is hard to say what you want to do there. By the looks of it, I would put memory allocation for <code>moments</code> before the loop and free memory after the loop - you keep allocating/freeing same amount of memory. Do you even need it on heap? Can you not just put it on stack? It also seems that you want to keep working with camera until user pressed a key. Do you need to release camera AFTER user pressed a key or every time before you start waiting for it? In any case, your <code>return</code> statement exits the loop and <code>main</code> (and program) before control goes to <code>if</code> statement. Put <code>return</code> statement OUTSIDE the loop.</p> <p>In short: </p> <p>Before loop - allocate memory. Remember to use <code>malloc/free</code> or <code>new/delete</code>, but do not mix them. Think if you need heap at all, maybe stack is ok.</p> <p>In loop - remove freeing memory, remove releasing camera, remove <code>return</code>. You will be using them on the next go through the loop, right? If user presses a key (inside the loop), you'll go out of the loop.</p> <p>After the loop - now you really want to clean up. Free memory, release camera, <code>return</code> from <code>main</code>.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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