CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    Jun 2010
    Posts
    50

    Question Heap corruption detected while freeing memory

    I have following code in which I am getting "Heap corruption detected" message while trying to freeing memory allocated while using strdup().
    Code:
    #include <iostream>
    #include <string>
    #include "Check.h"
    
    
    static void main()
    {
    
    	
    for (int i = 0;i< 5 ;i++)
    {
    char cname[32];
    cname = GetGroupName()//These function assigns name here
    bool exist;
    	exist =	GetGroupStatus(cname);
    }
    
    }
    
    bool GetGroupStatus(char *cname)
    {
    	char * cTemp = NULL;
    	cTemp = _strdup(cname);
    	bool bGrpStatus; 
    	size_t found;
    
    	//some code here
    
    	std::string sTemp = cTemp;
    	
    	found = sTemp.find("GROUP");
    
    	if (found != string::npos)
    		bGrpStatus = true;
    	else
    		bGrpStatus = false;
    
    	if (cTemp  != NULL)
    	free(cTemp);	//These causes heap corruption detected message when control comes here second time
    
    	return bGrpStatus; 
    }
    Please let me know what I am missing?
    Last edited by cilu; January 10th, 2011 at 04:17 AM. Reason: code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured