Solved

How do I release a pointer to a sturcture in C++?

Posted on 2007-11-26
6
827 Views
Last Modified: 2008-02-01
Hi,
I declare a pointer to a sturcture as follow:
IDispatch *pDisp=NULL;
At the clean up section of my code, how can I safely release this pointer?  My current code is getting me a error message of "Freeing freed memory"
if(pDisp)
    pDisp->Release;

Thank you.

0
Comment
Question by:lapucca
6 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 300 total points
ID: 20353499
You should nullify your pointer after calling release.


if(pDisp)

{

    pDisp->Release;

    pDisp = NULL;

}

Open in new window

0
 

Author Comment

by:lapucca
ID: 20353561
Really?  Do I have to do that for all pointer to anything or just structure?  I have to go over my code and correct all of them if so.  Let me try this and let you know if that would take care of my error.  Thank you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20353587
Actually, that should be

if(pDisp)
    pDisp->Release();

Without the brackets, the statement is meaningless.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 53

Expert Comment

by:Infinity08
ID: 20353715
>> Do I have to do that for all pointer to anything or just structure?

Yes, otherwise, this check won't catch that the pointer has already been released :

        if(pDisp)
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 200 total points
ID: 20356490
Technically, the pointer variable stores the addres of the memory space where the pointed thing is located. The NULL is a special constant that (technically zero) that says "pointing to nowhere".

In your case, you are pointint to the COM Dispatch interface, which is an object that represents the interface of some COM object to be accessed usually from an interpreted language.

If the pDisp were set to NULL, the pDisp is interpreted as false in a boolean context (the if condition). Otherwise, it is interpreted as true in the boolean context. In the body of the if control structure, you are calling the method of the pointed object (i.e. pDisp->Release();)  If the object exists, then it is fine. If the object does not exist, then it is an error.

Actually, you do not need to assign NULL to a pointer after deleting the pointed object if you are sure that the pointer is never to be used any more. However, you never know who and how the source code will be modified later. The rule of thumb is to always assign NULL to pointers that should be invalidated.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20358729
Funny, so that worked using

pDisp->Release;

?

Wow, C++ never stops surprising me...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

919 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now