push_back方法崩溃. [英] push_back method crashing.

查看:247
本文介绍了push_back方法崩溃.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个非常奇怪的问题.

我正在使用向量类来存储数据.在我测试过的所有系统中,
我的程序运行正常,但Windows 7 32位系统除外.

Visual Studio通过"free.c"的某些功能提供崩溃点
我已经验证我正在将正确的数据传递给push_back函数调用.

我试图将函数调用放在try catch块中.
但这也没有用.

任何关于撞车原因的想法都值得欢迎.
一个解决方案会让我开心不已.
我的代码是这样的

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

vector<DEV_INFO> vInfo;
...
....
DEV_INFO stDevInfo;          // User defined structure
FillDevInfo(&stDevInfo);     // User defined function call
...
...
vInfo.push_back(stDevInfo);  // This line crashing

推荐答案

我认为Emilio处在正确的轨道上.在free.c内部崩溃的事实表明操作push_back()尝试-失败-释放结构DEV_INFO中的一些内部指针.

push_back做两件事:
1.确保引导程序提供足够的内存(必要时重新分配).
2.使用您传递的值构造新元素.

有趣的是后者.它将使用就地构造语法从您的类中调用复制构造函数.如果您自己提供了一个副本构造函数,则该副本构造函数将被调用.但是,如果您没有这样做,那么编译器将自动提供它.然而,这个自动构造函数对您的内部结构一无所知,它只会执行简单的位复制.我看不到它将如何调用某些删除操作,因此很可能是您自己提供了一个复制构造函数.检查此代码中的删除操作,或检查将间接调用删除的函数调用,因为这很可能导致问题.如果不确定,请发布复制构造函数的代码DEV_INFO::DEV_INFO(const DEV_INFO&)
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&)


问题最有可能出在DEV_INFO复制构造函数和析构函数,以及它的成员.
还要检查DEV_INFO的默认构造函数.

重现您问题的方式可能就是这个
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
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; }

};



在这种情况下,赋值复制和析构函数假定p指针为null或有效.
但是默认构造函数不为其提供任何初始化.
在许多系统上,动态分配的内存在被分配给程序之前被初始化为零",因此p被incidetaly设置为NULL. 但是其他编译器(尤其是在调试版本中)会用随机数据预填充指针.因此delete p 将更加值得信赖.

可以使用
正确纠正此样本



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() {}


因此给p一个空值.

另一个可能的问题是default-ctor,未实现复制和分配(因此p,只是被复制,但是所有复制都指向相同的地址),而dtor会删除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.


由于用户访问控制,在Windows 7中进行编程非常困难.
能否请您共享代码以获取stDevInfo的值.
在执行Visual Studio IDE时,还有另一件事,那就是它应该以管理员身份打开并进行调试.
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.


这篇关于push_back方法崩溃.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆