# cv::error: (-215:Assertion failed) total() == 0 || data != NULL in function 'Mat::create'

- **ID:** `opencv/core-mat-initialization-size-overflow`
- **Domain:** opencv
- **Category:** assertion_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Creating a cv::Mat with extremely large dimensions (e.g., width or height exceeding memory limits) causes memory allocation failure or overflow, leaving data pointer null.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.1 | active | — | — |
| 4.9.0 | active | — | — |

## Workarounds

1. **Validate dimensions before creating the matrix: if (width <= 0 || height <= 0 || width > 10000 || height > 10000) { /* handle error */ }** (90% success)
   ```
   Validate dimensions before creating the matrix: if (width <= 0 || height <= 0 || width > 10000 || height > 10000) { /* handle error */ }
   ```
2. **Use cv::Mat::reshape or tiling to process large images in chunks, e.g., cv::Mat tile = img(cv::Rect(0, 0, 1000, 1000));** (85% success)
   ```
   Use cv::Mat::reshape or tiling to process large images in chunks, e.g., cv::Mat tile = img(cv::Rect(0, 0, 1000, 1000));
   ```

## Dead Ends

- **Using cv::Mat::zeros with the same large size hoping it handles memory differently** — zeros() internally calls create() with the same allocation logic. (100% fail)
- **Reducing the number of channels to 1 to save memory** — If the dimensions are already too large, reducing channels may not be enough; the size itself is the issue. (60% fail)
- **Allocating on GPU using cv::cuda::GpuMat** — GPU memory is even more limited; the same overflow can occur if dimensions exceed GPU memory. (75% fail)
