标签: free

自删除按钮

我有一个 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)

delphi free panel self button

0
推荐指数
1
解决办法
341
查看次数

使用指针并且没有使用`free()`并且没有内存泄漏

在我的程序中,我使用了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 malloc free gcc address-sanitizer

0
推荐指数
1
解决办法
273
查看次数

这个 malloc/free 对是否泄漏内存?

我是 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)

这个程序会泄漏几个字节的内存吗?

c malloc free memory-leaks

0
推荐指数
1
解决办法
123
查看次数

为变量分配对象引用

我有一个关于在这里分配的问题,我想知道

  1. 是否Assign制作整个对象的副本和
  2. 我想知道是否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)

delphi free reference object assign

-1
推荐指数
1
解决办法
1336
查看次数

C - 释放一定数量的内存(char**)

我必须安全地释放一个数组: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**

c memory arrays free

-1
推荐指数
1
解决办法
748
查看次数

内存管理c

所以我知道mallocs适用于堆而不是堆栈.

我有几个问题:

是否分配或释放了这些代码?

我没有看到任何自由语句,所以我假设没有任何东西被释放,所以内存泄漏是正确的?

只是为了好奇,如果我试图释放ptr,它会崩溃.如果我尝试释放它,它工作正常.

  int num;
        int *ptr;
        int **handle;

        num = 14;
        ptr = (int *)malloc(2 * sizeof(int));
        handle = &ptr;
        *ptr = num;
        ptr = &num;
        handle = (int **)malloc(1 * sizeof(int **));
Run Code Online (Sandbox Code Playgroud)

c free

-1
推荐指数
1
解决办法
100
查看次数

如果在C中更改指针,将释放多少内存?

假设有20个内存块,指针p指向第一个块.现在,当我这样做时:

p++;
free(p);
Run Code Online (Sandbox Code Playgroud)

将释放多少块内存,为什么?

c memory free

-1
推荐指数
1
解决办法
760
查看次数

调用free()会导致程序崩溃

我有一个非常奇怪的问题,试图在分配的内存上调用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 crash malloc free printf

-1
推荐指数
1
解决办法
3131
查看次数

使用c中的free()进行分段错误

我试图用我的一些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)

c memory free segmentation-fault

-1
推荐指数
1
解决办法
809
查看次数

malloc/free(),错误信号6

这是一个基本的堆栈实现代码.但是,它会产生信号中止.

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)

c memory heap malloc free

-1
推荐指数
1
解决办法
511
查看次数