tags:

views:

131

answers:

2

Hello, This c++ code is working fine , however memory validator says that I am using a deleted pointer in: grf->filePath = fname; Do you have any idea why ? Thank you.

Dirloader.h

  // Other code  
  class CDirLoader  
    {
    public:    
     struct TKnownGRF
     {
      std::string filePath;
      DWORD encodingType;
      DWORD userDataLen;
      char *userData;
     };
       // Other Code

     CDirLoader();
     virtual ~CDirLoader();

Dirloader.cpp

// Other code
void CDirLoader::AddGroupFile(const std::string& _fname)
{
// Other code including std::string fname = _fname;
TKnownGRF *grf = new TKnownGRF;
grf->filePath = fname;
delete grf; // Just for testing purposes

P.S.: This is only an code extract. Of course if I define a struct TKnownGRF inside .cpp and use it as an actual object, gfr.filepath = something, instead of pointer grf->filepath=something, than it is ok, but I do need to have it inside *.h in CDirLoader class, due to many other vector allocations.

A: 

Since the function returns void

void CDirLoader::AddGroupFile(const std::string& _fname)

the question is what are you going to do with grf?

Are you going to delete it? If so, then, why do a new? you can just declare a TKnownGRF variable on the stack! In that case, _fname is not contributing to the logic of this method.

I guess that the class CDirLoader has a member variable of type TKnownGRF, say grf_, and that need to be used in the AddsGroupFile() method, e.g.:

grf_.filepath = _fname;
ArunSaha
There is std::vector<TKnownGRF*> grfs; in Dirloader.h Quote" the question is what are you going to do with grf? Are you going to delete it?" No, I am using grfs.push_back(grf); at the end of void CDirLoader::AddGroupFile CDirLoader deosnt have a member of TKnownGRF except grfs vectors and a few others. Also if I define another TKnowGRF struct inside my *.cpp I could use gfr.filepath insdead of gfr->filepath. That solves the problem with validator, but how do I push_back that into a globally used vector of an original TKnowGRF *.
en667
For everybody's benefit, please update the question with these additional and valuable details. Globals are bad, but if you have no choice, then you have to use `grfs`, and it seems `grfs.push_back( grf );` is valid. There must be some code to de-allocate `grfs` (e.g. `grfs.clear()`) though.
ArunSaha
Quote " If so, then, why do a new?" Without new I will get warning: arning C4700: local variable 'grf' used without having been initialized
en667
Ad > de-allocate grfs Thank you for an idea however I just tried to comment grfs.push_back( grf ); and put there simple delete grf; just for testing purposes and validator still reports an critical error in grf->filePath
en667
A: 

Does this happen to be using an older version of STL, say, VC6, and running multithreaded? Older versions of STL's string class used a reference counted copy on write implementation, which didn't really work in a multithreaded environment. See this KB article on VC 6.

Or, it's also possible that you are looking at the wrong problem. If you call std::string::c_str() and cache the result at all, the cached result would probably be invalidated when you modified the original string. There are a few cases where you can get away with that, but it's very much implementation specific.

MSN
Thank you for an answer. However we are using VC 7.1 and multithreaded should be fine there. (Hopefully :-))
en667
The second idea I am not sure but I do not see there any problematic parts. Could you be please more specific ? Additional string related code is: 1 std::string fname = _fname; 2 LoCase(fname); 3 FILE *f = fopen(fname.c_str(), "rb");
en667