Home > Back-end >  Why in the DLL malloc () memory access violation
Why in the DLL malloc () memory access violation

Time:05-15

All bosses:

Recently met a problem, when developing a DLL, in which the malloc () the memory, but when access the access violation, want to consult everybody bosses may be the reasons for this?

 DLLEXT long AMI_Init (void * * AMI_dll_memory) 
{
Mem=(struct model_memory *) malloc (sizeof (struct model_memory));
Mem - & gt; Submem=(struct submem *) malloc (sizeof (struct submem));
.
* AMI_dll_memory=(void *) mem;
}

DLLEXT long AMI_Get (void * AMI_dll_memory)
{
.
AMI_dll_memory mem=(struct model_memory *);
Mem - & gt; Submem - & gt; init();
}

//Defined in submem module
Struct {
The int data;
Struct list * next;
} the list;

Void init ()
{
Struct list * n.
N=(struct list *) malloc (sizeof (struct list));
//access n - & gt; The data under caused the memory access violation.
}



DLL will be other programs call, the first is to call AMI_Init () to initialize the part of memory, and memory of the first address AMI_dll_memory passed to the parameters, and then invokes the AMI_Get (), then I'm AMI_Get () in the init () function on the malloc (), a new node direct access violation, but here I don't understand is why, want to consult everybody who is this why? I'm sure this list n nodes malloc () is successful, but when I try to access, access violation, very thank you for your comment,

CodePudding user response:

Debugging, whether n is empty?

CodePudding user response:

C the Run - Time Libraries https://msdn.microsoft.com/en-us/library/abx4dbyh.aspx

CodePudding user response:

reference 1/f, the truth is more important than right or wrong response:
debugging, judge whether n is empty?
thank you, n is not empty, allocate a success, but don't have access to,

CodePudding user response:

refer to the second floor 4 teacher zhao response:
C Run - Time Libraries https://msdn.microsoft.com/en-us/library/abx4dbyh.aspx
thank you for reply, but I feel I had this problem and CRT too big relationship, because I'm in the DLL AMI_init () function also malloc () memory, can normal visit, but in AMI_Get () function of malloc () is not line,
  • Related