Memory management by interfacing C/C++

Ferhat Kurtulmuş aferust at gmail.com
Sat Apr 27 22:25:58 UTC 2019


Hi,

I am wrapping some C++ code for my personal project (opencvd), 
and I am creating so many array pointers at cpp side and 
containing them in structs. I want to learn if I am leaking 
memory like crazy, although I am not facing crashes so far. Is GC 
of D handling things for me? Here is an example:

```
//declaration in d
struct IntVector {
     int* val;
     int length;
}

// in cpp
typedef struct IntVector {
     int* val;
     int length;
} IntVector;

// cpp function returning a struct containing an array pointer 
allocated with "new" op.
IntVector Subdiv2D_GetLeadingEdgeList(Subdiv2D sd){
     std::vector<int> iv;
     sd->getLeadingEdgeList(iv);

     int *cintv = new int[iv.size()]; // I don't call delete 
anywhere?

     for(size_t i=0; i < iv.size(); i++){
         cintv[i] = iv[i];
     }
     IntVector ret = {cintv, (int)iv.size()};
     return ret;
};

// call extern c function in d:
extern (C) IntVector Subdiv2D_GetLeadingEdgeList(Subdiv2d sd);

int[] getLeadingEdgeList(){
     IntVector intv = Subdiv2D_GetLeadingEdgeList(this);
     int[] ret = intv.val[0..intv.length]; // just D magic. Still 
no delete anywhere!
     return ret;
}
```

The question is now: what will happen to "int *cintv" which is 
allocated with new operator in cpp code? I have many code similar 
in the project, but I have not encounter any problem so far even 
in looped video processings. Is GC of D doing deallocation 
automagically?
https://github.com/aferust/opencvd


More information about the Digitalmars-d-learn mailing list