Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
I am facing a very odd problem.

I am using vector class to store data. In all the system I have tested,
my program working fine except a Windows 7 32 bit system.

Visual studio giving the crash point in some function of "free.c"
I have verified I am passing proper data to the push_back function call.

I have tried to put the function call in a try catch block.
But that also didn't worked.

Any kind of idea on the reason of the crash is welcome.
A solution will make me more than happy.
My code is something like this
XML
vector<DEV_INFO> vInfo;
...
....
DEV_INFO stDevInfo;          // User defined structure
FillDevInfo(&stDevInfo);     // User defined function call
...
...
vInfo.push_back(stDevInfo);  // This line crashing
Posted
Updated 22-May-11 19:56pm
v2
Comments
   
Not clear. vector::push_back, list::push_back? in what code? What "did not work"? For a software developer, there is no such things; there are compile-time errors, exceptions, exception stack and other things which constitutes some valid issue report. With your information, it's not possible to tell you anything certain.
--SA

I think Emilio is on the right track. The fact that you crash inside free.c indicates that the operation push_back() tries - and fails - to free some internal pointer inside the struct, DEV_INFO.

push_back does two things:
1. Ensures that the vector provides sufficient memory (reallocate if neccessary).
2. Construct the new element, using the value you passed.

The interesting part is the latter. It will call the copy constructor from your class, using the in-place construction syntax. If you provided a copy constructor yourself, that one will be called. But if you didn't, then the compiler will provide it automatically. This automatic constructor howver knows nothing of your internal structure, it will only perform a simple bit-copy. I can't see how this would invoke some delete operation, so, most likely, you provided a copy constructor yourself. check this code for a delete operation, or a function call that will indirectly invoke a delete, because that is what likely causes the problem. If you're unsure, post the code of your copy constructor DEV_INFO::DEV_INFO(const DEV_INFO&)
   
Comments
Emilio Garavaglia 23-May-11 15:33pm
   
To be more precise, the compiler generated copy calls in turn the bases copy and the members copy. For member that has no base and no copy, it does a raw bytes copy.

In essence, even if there is no supplied copy ctor for the given struct, the problem may reside in one of its members.
Stefan_Lang 24-May-11 3:42am
   
Correct, but as I see no way this can cause the error, I didn't bother to elaborate (or look it up in the first place). My point was that the error was triggered in free.c, indicating that someone somewhere tried to free/delete an invalid object (or invalid pointer, rather). I can see no reason why auto-generated code for a constructor would ever need to free/delete anything, so my conclusion was that there is a user-provided one that does it.

The only way the auto-generated copy constructor can cause this is if DEV_INFO has additional members, not shown in the code above, that have user-defined copy-constructors. But I'm not going to point out possible problems based on code that isn't even there.
The problem most likely resides in the DEV_INFO copy constructor and destructor, as well in the copy constructors and destructor of its members.
Check also the default constructor of DEV_INFO.

On way to reproduce your problem may be this one
C++
class AClass
{
  //details don't matter, here
};

struct DEV_INFO
{
    AClass *p;

    DEV_INFO() {} //Note: no p initialization. Default implementation is like that

    ~DEV_INFO() { delete p; }

    DEV_INFO(const DEV_INFO& a)
    { p = a.p? new AClass(*a.p): 0; }

    DEV_INFO& operator=(const DEV_INFO& a)
    { delete p; a.p? p = new AClass(*a.p): 0; }

};


In this situation, assignment copy and destructor assume that the p pointer is either null or valid.
But the default constructor doesn't provide any initialization for it.
On many systems, dynamic allocated memory is initialized to "zero" before it is given to the program, so p is incidetaly set to NULL.
But other compiler (especially in debug release) pre-fill the pointer with random data. So delete p will be anymore trustable.

This sample can be properly corrected with
DEV_INFO::DEV_INFO() :p() {}

thus giving to p a null value.

Another possible problem can be default-ctor, copy and assign not implemented (so p, is simply copyed, but all copy point to a same address) and dtor does "delete p".

You didn't give enough information about the way your structure behaves. But those are the things that should be first investigated.
   
Comments
lijiantao 23-May-11 6:01am
   
delete p, not init p
Stefan_Lang 23-May-11 10:13am
   
You're on the right track, but the line causing the error is the call to push_back(), so the only function involved is the copy constructor (I've checked the actual code!). This is where the error has to be.

See my more detailled answer below.
Programming in Windows 7 is quite difficult because of User access control.
Can you please share the code to get the value of stDevInfo.
One more thing when you execute the Visual Studio IDE than it should be open as administrator and debug it.
   
Comments
Debojyoti Majumder 23-May-11 3:13am
   
Thanks for your views.

Actually, I forgot to mention the code is working correctly in Debug mode but crashing in release mode.

stDevInfo is getting filled properly.
I have checked it.
I don't know how big your DEV_INFO structure is or what it contains, but I suspect this is not the best way to use a vector. My preferred method in cases like this is to allocate each structure on the heap (via new) as required, fill it with the relevant data and then use the vector to store a pointer to the newly created structure or class element. The fact that your application appears to crash in free.c on a push_back() call, suggests that the vector is trying to manipulate the structure and gets its wires crossed somehow.
   
Comments
Debojyoti Majumder 23-May-11 5:48am
   
The structure is not that big. It just contains two strings.
markkuk 23-May-11 6:52am
   
C++ std:strings or C-style char* strings?
Debojyoti Majumder 24-May-11 2:03am
   
C-style char*
Richard MacCutchan 24-May-11 3:55am
   
Please edit your original question and show the details I asked for below. One line comments such as this give us no useful information whatsoever.
Richard MacCutchan 23-May-11 7:13am
   
Try showing the details of both the structure and the function that fills it. Just edit your question and add the information.
Hi,

Is your vector declared at global scope ? is it a static variable ?

As per my knowledge static/global vector always causes a runtime error. Its better to wrap it in a class. You can make object of this clas as a global variable.
   
Comments
Debojyoti Majumder 23-May-11 5:35am
   
No, the variable is in local scope.. and it's not static.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900