0xC0000005: Access Violation. when working with a lot of points - visual-c++

I must use an old and crappy VC6.0 software.
This part of code process points, for a kind of 3D printer.
When I got a lot of points, (>2 000 000), it fails with "0xC0000005: Access Violation"
I'm really stuck, I have no idea how to fix this. The code is not from me.
Could it be too much malloc, with memory leaks ?
P_NOEUDP cstr_noeudp()
{
P_NOEUDP N;
N=(P_NOEUDP) malloc(sizeof(NOEUDP));
N->classe=NULL; //Here it is 0xC0000005: Access Violation.
N->face=NULL;
N->arete_isolee=NULL;
N->critere=NULL;
N->data_list=NULL;
return(N);
}
This is called in :
P_SLICE_POINT tpp_Slice_Point_Cstr()
{
P_SLICE_POINT pSPt;
pSPt = (P_SLICE_POINT) malloc(sizeof(SLICE_POINT));
pSPt->Node=cstr_noeudp();
pSPt->Edge=NULL;
return pSPt;
}
Here are the headers :
struct S_NOEUDP
{
int numero;
double courbureGaus;
double X[3];
CLASSEMENT *classe;
LISTE *face;
LISTE *arete_isolee;
LISTE *critere;
P_DATA_LISTE data_list;
};
typedef struct S_NOEUDP NOEUDP;
typedef NOEUDP *P_NOEUDP;
struct S_CLASSEMENT
{
int type;
int etat;
int situation;
};
typedef struct S_CLASSEMENT CLASSEMENT;
typedef CLASSEMENT *P_CLASSEMENT;
struct S_LISTE
{
int type_liste;
int type_occurence;
void *occurence;
int type_reference;
void *reference;
struct S_LISTE *svt;
struct S_LISTE *prec;
};
typedef struct S_LISTE LISTE;
typedef LISTE *P_LISTE;
struct S_DATA_LISTE
{
P_LISTE data_liste;
};
typedef struct S_DATA_LISTE DATA_LISTE ;
typedef DATA_LISTE *P_DATA_LISTE ;
typedef struct S_SLICE_POINT SLICE_POINT;
typedef SLICE_POINT *P_SLICE_POINT;

So, crappy code, full of memory leaks and no error control...
What's happening next :
Memory fills up because of memory leaks (malloc without freeing)
Memory usage reachs the 32 bit limits, could no more allocate
Code hangs, and we don't know why, because there's no malloc return check

Related

Why doesn't file->private_data return your context data?

I'm reading the De Los Rios book on Linux Kernel Driver development and hit the container_of macro.
I get the fact that if I want to keep context data:
struct myContextData_t {
... myData ...
struct miscdevice misc;
... more myData...
};
I can then:
myContextData = devm_kzalloc(&pdev->dev, sizeof(struct myContextData_t), GFP_KERNEL);
.. set data in misc ..
ret_val = misc_register(&myContextData->misc);
And when a read function gets called:
static ssize_t read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
{
...
// file->private_data points to &myContextData->misc, not to &myContextData, so figure out
// where myContextData is
myContextData = container_of(file->private_data, struct myContextData_t, misc);
...
}
What I'm trying to figure out is why can't I just register my private data when I register my device?
I.E. Why not something like:
struct myContext_t {
... myPrivateData ...
};
struct myContext_t myContext;
struct miscdevice myMiscDevice {
.minor = ...,
.name = "mydevice",
.fops = &myfops,
.context = &myContext,
}
And then:
static ssize_t read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
{
myContext_t myContext = (myContext_t *) file->privateData->context;
}
Rather than having to figure out where my miscdevice data belongs in my structure and 'backtrack'? I realize container_of is a macro evaluated at compile time which will probably boil down to one subtract at runtime. Usually things like that are either deliberate to save space/managed memory entries or relics of prior decisions which weren't futureproofed and ended up propagating to avoid requiring rewrites. It also, I gather, requires a GNU-specific which isn't necessarily available in other compilers, making porting the kernel difficult. I'm just curious what the reasoning was in this case.

Helping with void *__ctx[] CRYPTO_MINALIGN_ATTR in struct aead_request (Crypto Subsystem in Linux Kernel)

I'm a newbie in Linux driver. Now, I am testing my Linux crypto driver. However, I have some kernel crash bug related to this pointer array. In my opinion, this pointer array is related to a Variable Length Array In Struct (VLAIS). I did some research about how void *__ctx[] CRYPTO_MINALIGN_ATTR work but still didn't understand. Can anyone help me clarify this type and how it work? Thank in advance.
This is the location of this type (include/crypto/aead.h):
struct aead_request {
struct crypto_async_request base;
unsigned int assoclen;
unsigned int cryptlen;
u8 *iv;
struct scatterlist *src;
struct scatterlist *dst;
void *__ctx[] CRYPTO_MINALIGN_ATTR;
};

Shared Memory giving ambiguous results

I was trying to communicate between two processes using Shared Memory concept. But here, though I have pointed the shared memory addresses of different variables to different files, they seem to be connected. As soon as I alter value of one variable, the new value overwrites on other variable too, in this case, se1->val and se2->val are coming out to be connected. Can someone help why it's happening so?
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#define s(t) scanf("%d",&t)
#define p(t) printf("%d ",t)
struct sem
{
int val;
int xy;
};
struct sem* se1;
struct sem* se2;
int main()
{
printf("You in P1\n");
key_t key1,key2;
key1=ftok("shmfile1",0);
key2=ftok("shmfile3",0);
int shmid1=shmget(key1, sizeof(struct sem),0644|IPC_CREAT);
int shmid2=shmget(key2, sizeof(struct sem),0644|IPC_CREAT);
se1=shmat(shmid1,NULL,0);
se2=shmat(shmid2,NULL,0);
se1->xy=4;
se2->xy=8;
se1->val=0;
se2->val=1;
int r=10;
while(r--)
{
printf("\nIn P1 process ");
while(se2->val==0);
se2->val--;
se1->xy=se2->xy+1;
se1->val++;
p(se1->xy);
p(se2->xy);
}
return 0;
}
It is expected se1->val and se2->val will lead to semaphore type results, but due to overwriting it's not happening!

Having a struct declaration as the function parameter in VC++

I'm using asn1c to generate C++ encoder/decoder codes. There is a problem in the generated code (which is a huge code) preventing it from compile on VC++ which I try to make it simple here:
There is A_SET_OF macro in the generated code defined as:
#define A_SET_OF(type) \
struct { \
type **array; \
int count; /* Meaningful size */ \
int size; /* Allocated size */ \
void (*free)(type *); \
}
This macro is later used in several parts of code. For instance:
A_SET_OF(struct MyStructure {
long myNumber;
char* myPointer;
} ) myList;
I get a C2226: syntax error : unexpected type error on these parts. To find what actually is causing the problem, I substituted the actual macro definition:
struct {
struct MyStructure { long myNumber; char* myPointer; } **array;
int count; /* Meaningful size */
int size; /* Allocated size */
void (*free)(struct MyStructure { long myNumber; char* myPointer; } *);
} myList;
The error is caused by the void(*free) line. Apparently, having struct declaration as a function parameter is a non-standard feature which VC++ do not support (and gcc probably supports as asn1c primary targets Linux).
I am looking for a workaround to this issue, preferably fixing the A_SET_OF macro definition as there are many references to it. typedefing the struct passed to free function might solve this issue but I am not sure how it could be done for these anonymous structures.

Will memset vector to 0 cause memory leak?

I have a structure as followed:
typedef enum tBrowseType
{
catNowPlaying,
catFolder,
catFile,
catGenre
};
typedef struct tBrowseList
{
int total;
vector<tBrowseType>browseType;
};
After the initial use, I did a memset; will this memset cause memory leak?
If you've zeroed the vector<tBrowseType> object it is more likely to cause segfault. memset is not the way to treat decent c++ object.

Resources