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


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

Posted on 2007-11-26
Medium Priority
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
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
LVL 30

Accepted Solution

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

    pDisp = NULL;

Open in new window


Author Comment

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

Expert Comment

ID: 20353587
Actually, that should be


Without the brackets, the statement is meaningless.

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

LVL 53

Expert Comment

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 :

LVL 29

Assisted Solution

pepr earned 800 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.
LVL 86

Expert Comment

ID: 20358729
Funny, so that worked using



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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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