我有一个 TScrollBox,里面有一堆 TPanel,还有一些在运行时生成的 TButton。我需要在单击一个 TButton 时删除 TPanel,但在 OnClick 中执行此操作会导致访问冲突......
procedure TMainForm.ButanClick(Sender: TObject);
var
vParentPanel: TPanel;
begin
if (string(TButton(Sender).Name).StartsWith('L')) then
begin
TButton(Sender).Caption := 'YARE YARE DAZE';
end
else
begin
vParentPanel := TPanel(TButton(Sender).GetParentComponent());
TheScrollBox.RemoveComponent(vParentPanel);
vParentPanel.Destroy();
// access violation but the panel is removed
end;
end;
procedure TMainForm.Button3Click(Sender: TObject);
var
i: Integer;
vPanel: TPanel;
vButton: TButton;
begin
for i := 0 to 20 do
begin
vPanel := TPanel.Create(TheScrollBox);
vPanel.Align := alTop;
vPanel.Parent := TheScrollBox;
vButton := TButton.Create(vPanel);
vButton.Align := alLeft;
vButton.Parent := vPanel; …Run Code Online (Sandbox Code Playgroud) 在我的程序中,我使用了malloc()和 指针,但我没有用来free()释放这些内存,我已经使用-fsanitize=address标志进行了编译,但它说没有内存泄漏。据我所知,如果我分配内存,我还必须在程序结束时释放内存,否则会出现内存泄漏。
#include <stdio.h>
#include <stdlib.h>
void print2D(int rowSize, int **cols_size, int **arr) {
for(int i = 0; i < rowSize; i++) {
for(int j = 0; j < (*cols_size)[i]; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int** make2D(int rowSize, int **colSize) {
int** arr = (int**) malloc(sizeof(int*) * rowSize);
for(int i=0; i < rowSize; i++) {
arr[i] = (int*) malloc(sizeof(int) * 2);
}
*colSize = (int*) malloc(sizeof(int*) * 2);
(*colSize)[0] = …Run Code Online (Sandbox Code Playgroud) 我是 C 新手。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void demo() {
char* s = malloc(10);
strcpy(s, "foo");
free(s);
}
int main()
{
demo();
}
Run Code Online (Sandbox Code Playgroud)
这个程序会泄漏几个字节的内存吗?
我有一个关于在这里分配的问题,我想知道
Assign制作整个对象的副本和FTEA.Objects[0]也被释放了.我想复制一份,FTEA.Objects[0]当我获得自由时ObjCur,我不打算自由FTEA.Objects[0]- 不确定这样做的正确方法,需要你的帮助,谢谢:
function xxxxxxxxxxxxxxxxxxxxxxxx
var
curQuo, tempQuo:TXX_TEA;
begin
curQuo :=TXX_TEA(FTEA.Objects[0]);
if xxxxxxxxx then
begin
tempQuo := TXX_TEA.Create();
tempQuo.Assign(curQuo); // Question 1: Assign copy the whole object value or not
objCur.AddQuo(tempQuo)
end
else
TXX_TEA(objCur.Quos[0]).Assign(curQuo);
end;
finally
objCur.Free; // Question 2: FTEA.Objects[0] is freed or not
end;
end
Run Code Online (Sandbox Code Playgroud) 我必须安全地释放一个数组:char** a;它就像一个字符串列表.我知道char*我有多少人.但我无法释放所有内存.有没有像我可以用来释放20个字节的功能?我试过了:
for (int i = 0; i < length; i++)
if (a[i] != null)
free(a[i]); // some of a[i] ARE null, non-null have different sizes
free(a); // crashes here
Run Code Online (Sandbox Code Playgroud)
但我通过asm调试得到运行时错误.a中的所有东西都已经过了.对于一个I malloced 5个字符串(每个指针4个字节) - > 20个字节.我如何解放整个char**?
所以我知道mallocs适用于堆而不是堆栈.
我有几个问题:
是否分配或释放了这些代码?
我没有看到任何自由语句,所以我假设没有任何东西被释放,所以内存泄漏是正确的?
只是为了好奇,如果我试图释放ptr,它会崩溃.如果我尝试释放它,它工作正常.
int num;
int *ptr;
int **handle;
num = 14;
ptr = (int *)malloc(2 * sizeof(int));
handle = &ptr;
*ptr = num;
ptr = #
handle = (int **)malloc(1 * sizeof(int **));
Run Code Online (Sandbox Code Playgroud) 我有一个非常奇怪的问题,试图在分配的内存上调用free导致我的程序崩溃.
这是相关的代码:
int i, count;
char *specifier;
char aisle[1];
count = 0;
/*Find name of the new item and assign to the name field of new_node...*/
for (i = 0; input[i] != ','; i++){
count++;
}
specifier = (char*)malloc((count+1)*sizeof(char));
if (specifier == NULL){
printf("Out of memory. Shutting down.\n");
exit(EXIT_FAILURE);
}
for (i = 0; input[i] != ','; i++){
specifier[i] = input[i];
}
specifier[count+1] = '\0';
new_node->name = specifier;
printf("%s\n", new_node->name);
free(specifier); /*PROGRAM CRASHES HERE*/
printf("boom\n");
specifier = NULL;
/*Function continues …Run Code Online (Sandbox Code Playgroud) 我试图用我的一些C代码找到问题.调试器说当我尝试从指针释放mem时发生错误:
int main(int argc, char *argv[]){
char *desc = malloc(30 * sizeof(char));
if(desc == NULL)
{
fprintf(stderr, "Error - cannot allocate memory\n");
}
desc = "Debug this program.";
printf("Description: %s\n", desc);
free(desc);//break point here
int cpid = fork();
....
Run Code Online (Sandbox Code Playgroud) 这是一个基本的堆栈实现代码.但是,它会产生信号中止.
int *arr;
int size = 2;
int top = 0;
int pop() {
int i;
if (top <= size / 4) {
int *arr2 = (int*)malloc(sizeof(int) * size / 2);
for ( i = 0; i < size; i++)
arr2[i] = arr[i];
free(arr);
arr = arr2;
size /= 2;
}
return arr[--top];
}
void push( int a) {
int i;
if (top >= size) {
int *arr2 = (int*)malloc(sizeof(int)*size * 2);
for ( i = 0; i < size; …Run Code Online (Sandbox Code Playgroud)