Delphi 文档说明:
永远不要直接引发EInvalidPointer异常.EInvalidPointer由内存管理器在内部引发.
我正在编写一个自定义基类作为替代TInterfacedObject
,尽可能地遵循RTL实现,并且通过示例看,TInterfacedObject
在RTL实现BeforeDestruction
为:
procedure TInterfacedObject.BeforeDestruction;
begin
if RefCount <> 0 then
Error(reInvalidPtr);
end;
Run Code Online (Sandbox Code Playgroud)
其中Error(reInvalidPtr)
提出了EInvalidPointer
通过各种本地的RTL单元范围的方法.
如果我正在编写自己的课程,我应该如何实施BeforeDestruction
?为什么不这样做?:
procedure TMyInterfacedObject.BeforeDestruction;
begin
if RefCount <> 0 then
raise EInvalidPointer.CreateRes(@SInvalidPointer) at ReturnAddress;
end;
Run Code Online (Sandbox Code Playgroud)
InvalidPointer
声明的全局异常对象是否有特殊之处SysUtils
?如果这是一个坏主意,那么在这里简单地提出一个自定义异常是否明智?
在 anacondas py3.6 安装中的 ubuntu 服务器(100GB RAM)上以这种方式在 for 循环(7 个文件 * 4GB)中打开多个文件后出现此错误:
temp_df = pd.read_csv(datafolder + str(file), encoding="ISO-8859-1", delimiter=';',low_memory=False)
这是错误,它在我设置后出现
低内存=假
当 low_memory = True 时不会发生
* `python' 中的错误:free():无效指针:0x00007fc3c90dc98e *
任何想法?谢谢
根据此,获取无效指针的值是C++中实现定义的行为.现在考虑以下C程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* p=(int*)malloc(sizeof(int));
*p=3;
printf("%d\n",*p);
printf("%p\n",(void*)p);
free(p);
printf("%p\n",(void*)p); // Is this undefined or implementation defined in behaviour C?
}
Run Code Online (Sandbox Code Playgroud)
但是C中的行为也一样吗?上述C程序的行为是否未定义或实现定义?C99/C11标准对此有何看法?请告诉我C99和C11的行为是否不同.
简而言之,以下代码是否被认为具有未定义的行为?
int main()
{
int *p = <some invalid pointer value>;
}
Run Code Online (Sandbox Code Playgroud)
有关编译示例,请使用以下代码:
int main()
{
int *p = new int;
delete p; // Now p has an invalid pointer value.
int *q = p; // UB?
}
Run Code Online (Sandbox Code Playgroud)
我对这个主题做了一些研究,所以这些是我到目前为止发现的相关信息:
指针值(根据cppreference)可以是以下之一:
另外,根据cppreference,
通过无效指针值间接并将无效指针值传递给释放函数具有未定义的行为.无效指针值的任何其他使用都具有实现定义的行为.
该线程解决了无效指针的一些用途.具体而言,这个答案提到了基本原理文件(C99),其中有以下段落(第6.3.2.3节):
无论如何创建无效指针,任何使用它都会产生未定义的行为.甚至赋值,与空指针常量的比较或与自身的比较,在某些系统上可能会导致异常.
我不确定C++的状态是什么,但我认为,鉴于链接线程的答案,使用无效指针会导致未定义的行为.但请注意,该分配与初始化不同,因此我不确定初始化是否被视为一种用法.
我查看了其他类似的问题,但无法找出导致此错误的原因.我正在编写一个C++程序来实现Kruskal的最小生成树算法,使用Union by rank with path compression.它正确打印MST的边缘,但包括路径压缩部分导致此错误:
*./kruskal'出错:free():无效指针:0x0000000001650d00*
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, pair<int,int> > pip;
typedef pair<int,int> pii;
class UnionFind {
vector <int> parent,rank;
public:
UnionFind(int n){
parent.resize(n);
rank.resize(n);
for (int i=0;i<n;i++) { //initialize all parents and ranks
parent[i]=i;
rank[i]=0;
}
}
int find (int x){
int root=x,y;
while (parent[root]!=root) {
root=parent[root];
}
//uncommenting the following loop gives the error
/*while (parent[x]!=root){ //path compression
y=parent[x];
parent[x]=root;
x=y;
}*/
return root;
}
void unite (int …
Run Code Online (Sandbox Code Playgroud) 我正在XE3中创建一个新的应用程序,但使用在D2007中创建的一些单位.
我在释放TStringList数据项时遇到错误.这是创建数据项FSQL的代码:
procedure TPayorDM.DataModuleCreate(Sender: TObject);
begin
FPayorDRM := TDRM.Create;
FSQL := TStringList.Create;
end;
Run Code Online (Sandbox Code Playgroud)
这是获取错误的代码:
procedure TPayorDM.DataModuleDestroy(Sender: TObject);
begin
FreeAndNil(FSQL);
if T_Payor.Active then T_Payor.Close;
FreeAndNil(FPayorDRM);
end;
Run Code Online (Sandbox Code Playgroud)
错误发生在'FreeAndNil(FSQL);'上.我尝试了'FSQL.Free',我得到了相同的结果.
这是我得到的错误:
项目:PayorUpdate.exe引发异常类EInvalidPointer,消息为"无效指针操作".
当我打破蓝色箭头(调试模式)指向_FreeMem(指针(Self)); 在System单元中的TObject.FreeInstance过程中如下:
procedure TObject.FreeInstance;
begin
CleanupInstance;
_FreeMem(Pointer(Self));
end;
Run Code Online (Sandbox Code Playgroud)
如果我没有释放TStringList数据项,我会在应用程序中出现内存泄漏.
是否可能需要设置配置选项?我搜索谷歌并没有找到任何解释我做错的事情,除了三种可能之一:
如果我试一试......除了......我能解决这个问题,但我不想这样做.
顺便说一下,我有另一个TStringList在不同的单位,我创建和FreeAndNil,我没有得到任何错误.
这是完整的来源:
unit PayorDataMgr; interface uses SysUtils, Classes, Dialogs, NativeXML, adscnnct, DB, adsdata, adsfunc, adstable, ace, cbs.drm, cbs.utils, cbs.LogFiles; const POLICY_TYPES: array[1..3] of string = ('Primary','Secondary','Tertiary'); type TPayorRecord = Record ASSIGNBENEFITS: Boolean; AUTHORIZE: Boolean; BATCHBILL: Boolean; CLAIMMAX: Integer; …
我已经看到过类似的帖子,但是我发现了一些差异,使我误入歧途。
我有以下代码:
char * token_one = strtok(my_buffer, " ,.-");
char * token_two = strtok(NULL, " ,.-");
free(token_one);
free(token_two);
Run Code Online (Sandbox Code Playgroud)
我看到有人在帖子中说不应释放与strtok一起使用的变量,但是为什么在执行此代码时却得到此信息:
free(token_one)
没有错误
free(token_two)
我得到“ 无效指针 ”
为什么我没有收到错误消息free(token_one)
?处理此问题的正确方法是什么?
c ×2
c++ ×2
delphi ×2
pointers ×2
anaconda ×1
delphi-xe3 ×1
python ×1
python-3.6 ×1
strtok ×1
ubuntu ×1
union-find ×1