Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

properly delete pointer - hopefully a quickie

Posted on 2007-11-16
8
Medium Priority
?
1,315 Views
Last Modified: 2012-08-14
The loop below is toggled by a "preview" button. It show the images just fine, but it runs my memory up fast because if I put the line "cvReleaseImage(&pimage_copy) anywhere to release the memory, I get a passing null pointer even though I think I am destroying pointers as I create them. How do I reconcile? Should  I use smart pointers?


while (CameraStatusInst.Start_Button_Status(false, false))
	{
     IplImage* pimage_copy = contour_image;
 
                                    if (pimage_copy != NULL)
                                        {
                                         int key=0;
                                         cvNamedWindow("Mouse Vision", 1);
                                         cvShowImage("Mouse Vision", pimage_copy);
                                         key = cvWaitKey(20);
                                        //cvReleaseImage(&pimage_copy)    <-------------works when commented out
                                        }
                                        else
                                        {
                                        cvReleaseImage(&pimage_copy);
                                        }
 
 
    }

Open in new window

0
Comment
Question by:Wanderinglazyeye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 300 total points
ID: 20299404
Well, how are you initializing 'contour_image'? If you aren't using 'cvCloneImage()', there is no need to release it. See the demo code at http://softintegration.com/products/thirdparty/opencv/demos/
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20299510
try with:
while (CameraStatusInst.Start_Button_Status(false, false))
{
     IplImage* pimage_copy = cvCloneImage(contour_image);
 
    if (pimage_copy != NULL)
    {
          int key=0;
          cvNamedWindow("Mouse Vision", 1);
          cvShowImage("Mouse Vision", pimage_copy);
          key = cvWaitKey(20);
          cvReleaseImage(&pimage_copy)
    }
}
    

Open in new window

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20300036
>>>> cvReleaseImage(&pimage_copy)
you pass the 'address' of pimage_copy to cvReleaseImage. So cvReleaseImage takes a IplImage** ?? That is strange beside cvReleaseImage would expect an array of IplImage pointers rather than a single pointer.

>>>> while (CameraStatusInst.Start_Button_Status(false, false))
>>>> {
>>>>     IplImage* pimage_copy = contour_image;

I can't see how that in that while loop you ever would get a pimage_copy == NULL. Even then, why do you call cvReleaseImage(&pimage_copy); with the address of a NULL pointer. What do you expect from that?

I would guess the above loop would fail in the second iteration cause I can't see how contour_image could get a different value (or NULL) in the meantime.

Regards, Alex

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Assisted Solution

by:Deepu Abraham
Deepu Abraham earned 300 total points
ID: 20301870
Hi there,

IplImage* pimage_copy = contour_image;  ---> Seems like this is not filling the correct values here..(it may not be null but some junk values)

Could you be able to check what is happening inide the function cvReleaseImage()

Best Regards,
DeepuAbrahamK



0
 

Author Comment

by:Wanderinglazyeye
ID: 20302052
Its filling the correct values - the preview window opens up, and the expected images are seen. I removed the loop (pimage_copy == NULL), it works fine. yeah my brain farted.

IplImage* pimage_copy = cvCloneImage(contour_image);
Tried this and releasing it, get run time errors.

Problem is a memory leak when I open the preview window and stops when I close it. It jumps by 10k/second (about) until program crashes. I preview the images by using the sharing image  solution shown in another EE topic that was solved earlier.  My preview code is a thread launched from a GUI event.

Would it be helpful to anyone to post the entire thread?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 300 total points
ID: 20302078
Well, how are you creating 'clone_image' right now?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 900 total points
ID: 20303792
>>>> Would it be helpful to anyone to post the entire thread?
Yes, and maybe you can tell what is the idea behind the while loop? Or did you drop the loop?  
0
 

Author Comment

by:Wanderinglazyeye
ID: 20386975
I dropped the loop. In fact, I dropped the threads. I took your idea about the evaluating whether threads were necessary, and simply made two program versions. One with threads, one without. What I found was that the version without thread BLAZED. Still, it provided a learning experience.

Thanks ItsMe

WLE
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question