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

Posted on 2007-11-26
Last Modified: 2008-02-01
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"

Thank you.

Question by:lapucca
LVL 30

Accepted Solution

Axter earned 300 total points
Comment Utility
You should nullify your pointer after calling release.




    pDisp = NULL;


Open in new window


Author Comment

Comment Utility
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.
LVL 86

Expert Comment

Comment Utility
Actually, that should be


Without the brackets, the statement is meaningless.
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

LVL 53

Expert Comment

Comment Utility
>> 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 :

LVL 28

Assisted Solution

pepr earned 200 total points
Comment Utility
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.
LVL 86

Expert Comment

Comment Utility
Funny, so that worked using



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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now