各位大佬:
最近遇到一個問題,在開發 DLL 時,在其中 malloc() 了記憶體,但是訪問時出現 access violation,想求教各位大佬這個原因可能是什么?
DLLEXT long AMI_Init(void **AMI_dll_memory)
{
mem = (struct model_memory*)malloc(sizeof(struct model_memory));
mem->submem = (struct submem*)malloc(sizeof(struct submem));
......
*AMI_dll_memory = (void*)mem;
}
DLLEXT long AMI_Get(void *AMI_dll_memory)
{
....
mem = (struct model_memory*)AMI_dll_memory;
mem->submem->init();
}
// Defined in submem module
struct {
int data;
struct list* next;
}list;
void init()
{
struct list* n;
n = (struct list*)malloc(sizeof(struct list));
// access n->data caused memory access violation.
}
DLL 會被其他程式呼叫,第一次是呼叫 AMI_Init() 初始化一部分記憶體,并且將記憶體的首地址傳遞給引數 AMI_dll_memory,然后會呼叫 AMI_Get(),這時我在 AMI_Get() 中的 init() 函式上 malloc() 了一個新的節點,但是這里直接出現了 access violation,我想不明白是為什么。想求教各位大佬這是為什么?我確定這個 list n 的節點 malloc() 成功了,但是當我嘗試訪問時,出現 access violation,十分感謝各位指教。
uj5u.com熱心網友回復:
除錯,判斷n是否為空?uj5u.com熱心網友回復:
C Run-Time Libraries https://msdn.microsoft.com/en-us/library/abx4dbyh.aspxuj5u.com熱心網友回復:
感謝回復,n 不為空,分配成功了,但是無法訪問。uj5u.com熱心網友回復:
感謝回復,但是我感覺我這個問題和CRT沒什么太大的關系,因為我在DLL的AMI_init()函式中也 malloc()了記憶體,都能正常訪問,但是在AMI_Get()函式中的malloc()就不行了。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283968.html
標籤:C語言